如果值为0则验证输入字段然后忽略

时间:2014-03-01 16:27:01

标签: javascript forms validation paypal

我需要帮助,如果值为“0”,我如何忽略输入字段..在javascript中

所以基本上如果值为“0”,请不要将内容发送到paypal,因为如果你发送“0”,paypal会给出错误

想法是让用户选择产品白色或黑色和数量..说用户只需要2个黑色项目和0个白色然后它应该只发送任何值为1或更多的东西到paypal。

我需要的是它的工作代码..我很乐意为你的代码发送PAYPAL捐款......

感谢您的帮助..

<form method="post" name="mainForm" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="george@360mix.com">
<input type="hidden" name="currency_code" value="USD">


<!--First Item-->
<p>
<input type="hidden" name="item_name_1" value="WHITE Helping Hand">
<input type="hidden" name="item_number_1" value="WHITE-HH">
<input type="hidden" name="amount_1" value="10.00">
 <select name="quantity_1" value="">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
</p>

<!--Second Item--> 
<p>
<input type="hidden" name="item_name_2" value="BLACK Helping Hand">
<input type="hidden" name="item_name_2" value="BLACK-HH">
<input type="hidden" name="amount_2" value="10.00">
 <select name="quantity_2" value="">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
</p>
<br>

<input type="hidden" name="return" value="http://www.360mix.com/thankyou.html">
<input type="image" src="http://images.paypal.com/en_US/i/btn/x-click-but22.gif" border="0" name="submit" width="87" height="23" alt="Make payments with PayPal - it's fast, free and secure!">
</form>

1 个答案:

答案 0 :(得分:0)

试试下面的例子吧。我们的想法是检查每个数量是否为零。如果没有,请指定下一个项目编号。

<html>
    <head>
        <script type="text/javascript">
            function prepareForm() {
                var index = 1;

                var q1 = document.getElementById('quantity_1').value;

                if (q1 > 0) {
                    document.getElementById('item_name_1').setAttribute('name', 'item_name_' + index);
                    document.getElementById('item_number_1').setAttribute('name', 'item_number_' + index);
                    document.getElementById('amount_1').setAttribute('name', 'amount_' + index);
                    document.getElementById('quantity_1').setAttribute('name', 'quantity_' + index);
                    index++;
                } else {
                    // reset (in case back button is pressed)
                    document.getElementById('item_name_1').setAttribute('name', '');
                    document.getElementById('item_number_1').setAttribute('name', '');
                    document.getElementById('amount_1').setAttribute('name', '');
                    document.getElementById('quantity_1').setAttribute('name', '');                 
                }

                var q2 = document.getElementById('quantity_2').value;

                if (q2 > 0) {
                    document.getElementById('item_name_2').setAttribute('name', 'item_name_' + index);
                    document.getElementById('item_number_2').setAttribute('name', 'item_number_' + index);
                    document.getElementById('amount_2').setAttribute('name', 'amount_' + index);
                    document.getElementById('quantity_2').setAttribute('name', 'quantity_' + index);
                    index++;
                } else {
                    // reset (in case button is pressed)
                    document.getElementById('item_name_2').setAttribute('name', '');
                    document.getElementById('item_number_2').setAttribute('name', '');
                    document.getElementById('amount_2').setAttribute('name', '');
                    document.getElementById('quantity_2').setAttribute('name', '');                 
                }

                if (index == 1) {
                        // in index is still one, then all quantities are zero
                        return false; // do not submit
                }

                return true;
            }
        </script>
    </head>
    <body>
<form method="post" name="mainForm" action="https://www.paypal.com/cgi-bin/webscr" onSubmit="return prepareForm();">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="george@360mix.com">
<input type="hidden" name="currency_code" value="USD">


<!--First Item-->
<p>
<input type="hidden" id="item_name_1" value="WHITE Helping Hand">
<input type="hidden" id="item_number_1" value="WHITE-HH">
<input type="hidden" id="amount_1" value="10.00">
 <select id="quantity_1">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
</p>

<!--Second Item--> 
<p>
<input type="hidden" id="item_name_2" value="BLACK Helping Hand">
<input type="hidden" id="item_number_2" value="BLACK-HH">
<input type="hidden" id="amount_2" value="10.00">
 <select id="quantity_2" value="">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
</p>
<br>

<input type="hidden" name="return" value="http://www.360mix.com/thankyou.html">
<input type="image" src="http://images.paypal.com/en_US/i/btn/x-click-but22.gif" border="0" name="submit" width="87" height="23" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
</body>
</html>