简单的表单验证

发布于 2024-10-05 06:13:43 字数 4435 浏览 0 评论 0原文

POST #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>

POST #2

函数 validate_form() { 有效=真;

    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>

POST #1

How can I validate this simple form (checking for empty field strings)?

                <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>

POST #2

function validate_form()
{
valid = true;

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

-=-=-=-= Form

        <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被你宠の有点坏 2024-10-12 06:13:43

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

Is jQuery an option? There's a really nice Validation tool http://docs.jquery.com/Plugins/Validation

浮生面具三千个 2024-10-12 06:13:43

一些提示应该可以帮助您继续。

添加提交事件处理程序:

<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>

A couple of hints that should get you going.

Add a submit event handler:

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

Give IDs to the input fields you want to get.

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

Write the validation code, return false if you don't want to submit.

<script type="text/javascript">

    function validate() {

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

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

        return ok;

    }

</script>
°如果伤别离去 2024-10-12 06:13:43

为您的表格命名(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>

Give your form a name (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>
最冷一天 2024-10-12 06:13:43

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

Here is a simple tutorial along with demo and source code, I hope this works for you, all the best!
http://gonga.in/simple-javascript-form-validation/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文