繁体   English   中英

简单表格验证

[英]Simple form validation

开机自检#1

如何验证这种简单形式(检查空字段字符串)?

                <p>Please select your Gift Certificate amount below. Please also enter your name and the Gift Certificate recipient's name. Once you click 'Buy Now' you will be sent to our Paypal site to complete the purchase.</p>

                    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_s-xclick">
        <input type="hidden" name="hosted_button_id" value="sdsafsdafsadfdsafsdafsadfsadfsadfasdfsadfsdaf">
        <table width="100%">
        <tr>
            <td width="130"><input type="hidden" name="on0" value="Amount"><p>Amount</p></td>
            <td><select name="os0">
            <option value="Option 1">$20.00</option>
            <option value="Option 2">$50.00</option>
            <option value="Option 3">$100.00</option>
            </select></td>
        </tr>
        <tr>
            <td><input type="hidden" name="on1" value="To:"><p>To (Full Name):</p></td>
            <td><input type="text" name="os1" maxlength="60" size="30"></td>
        </tr>
        <tr>
            <td><input type="hidden" name="on2" value="From:"><p>From (Full Name):</p></td>
            <td><input type="text" name="os2" maxlength="60" size="30"></td>
        </tr>
        </table>

        <table width="100%" style="margin-top: 10px;">
            <tr>
                <td><input type="hidden" name="currency_code" value="CAD">
        <p><input type="image" src="BUTTON.jpg" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"></p>
        <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></td>
                <td align="right"><img src="../paypal_logo.jpg" alt="PayPal" /></td>
            </tr>
        </table>
        </form>

开机自检#2

函数validate_form(){有效= true;

    if ( document.GiftForm.os1.value == "" )
    {
        alert ( "Please fill in the 'Your Name' box." );
        valid = false;
    }
    return valid;
}

-=-=-=-=表格

        <form action="https://www.paypal.com/cgi-bin/webscr" method="post" method="GiftForm" onsubmit="validate_form( )">
        <input type="hidden" name="cmd" value="_s-xclick">
        <input type="hidden" name="hosted_button_id" value="sdsafsdafsadfdsafsdafsadfsadfsadfasdfsadfsdaf">
        <table width="100%">
        <tr>
            <td width="130"><input type="hidden" name="on0" value="Amount"><p>Amount</p></td>
            <td><select name="os0">
            <option value="Option 1">$20.00</option>
            <option value="Option 2">$50.00</option>
            <option value="Option 3">$100.00</option>
            </select></td>
        </tr>
        <tr>
            <td><input type="hidden" name="on1" value="To:"><p>To (Full Name):</p></td>
            <td><input type="text" name="os1" maxlength="60" size="30"></td>
        </tr>
        <tr>
            <td><input type="hidden" name="on2" value="From:"><p>From (Full Name):</p></td>
            <td><input type="text" name="os2" maxlength="60" size="30"></td>
        </tr>
        </table>

        <table width="100%" style="margin-top: 10px;">
            <tr>
                <td><input type="hidden" name="currency_code" value="CAD">
        <p><input type="image" src="BUTTON.jpg" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"></p>
        <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></td>
                <td align="right"><img src="../paypal_logo.jpg" alt="PayPal" /></td>
            </tr>
        </table>
        </form>

jQuery是一个选择吗? 有一个非常不错的验证工具http://docs.jquery.com/Plugins/Validation

有一些提示可以帮助您前进。

添加一个提交事件处理程序:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" onsubmit="return validate()">

将ID赋予您想要获取的输入字段。

<input id="currency_code" type="hidden" name="currency_code" value="CAD">

编写验证码,如果不想提交,则return false

<script type="text/javascript">

    function validate() {

        var currencyCode = document.getElementById("currency_code");

        var ok = currencyCode.value !== '';

        return ok;

    }

</script>

这是一个简单的教程,以及演示和源代码,希望它对您有用,一切顺利! http://gonga.in/simple-javascript-form-validation/

给您的表格起一个名字(GiftForm)

    <script type="text/javascript">
    function validate_form ( )
    {
        valid = true;

        if ( document.GiftForm.os1.value == "" )
        {
            alert ( "Please fill in the 'Your Name' box." );
            valid = false;
        }

        return valid;
    }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM