【问题标题】:Check box validation code?复选框验证码?
【发布时间】:2013-05-25 10:46:00
【问题描述】:

我正在试验一个具有文本字段和复选框的表单。我是设计师而不是开发人员。我已经成功地让文本框验证,但我已经粘贴了复选框的代码,由于某种原因,它要么取消验证,要么在贝宝服务器上返回错误。任何人都可以建议几行代码来验证复选框“条款”吗?文本框称为“os0”

    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" id="myForm" onSubmit="return validateForm()" >
        <input type="hidden" name="cmd" value="_s-xclick">
        <input type="hidden" name="hosted_button_id" value="AXNKKDCZLVHM4">
        <table width="100%" border="0">
            <tr>
                <th scope="row"><input type="hidden" name="on0" value="Your RCEH Account Number:" />
                    Your RCEH Account Number:
                </th>
                <td>
                    <input type="text" name="os0" maxlength="200" />
                </td>
            </tr>
            <tr>
                <th scope="row">&nbsp;</th>
                <td><label>
                    <input type="checkbox" name="terms" id="terms" />
                    Agree to <a href="../terms.html" target="new">terms and conditions</a></label>
                </td>
                </tr>
        </table>
        <input type=submit value="Proceed to secure server">
        <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
    </form>
 </div>
</div>

<script>
    function validateForm() {
        var x=document.forms["myForm"]["os0"].value;
        if (x==null || x==""){
            alert("RCEH account number must be filled out");
            return false;
        }
    }
</script>

【问题讨论】:

  • 您想对复选框进行哪种类型的验证?
  • 感谢您回复我。 LightStyle 为我解决了这个问题。

标签: javascript validation


【解决方案1】:

假设您只想选中您的复选框,下面是您需要的代码,这是一个有效的JSFiddle

function validateForm() {
    var form = document.getElementById("myForm"); //get the form element via ID
    if (form.os0.value == "")
    {
        alert("RCEH account number must be filled out");
        return false;
    }
    if (!form.terms.checked) { // checks if the checkbox called "terms" is checked; if it is not, alerts a message and return false
        alert("Terms must be read and approved");
        return false;
    }
    return true; //if everything is okay, return true and submit the form
}

【讨论】:

  • 具有魅力的轻巧风格!非常感激!我曾尝试从互联网上粘贴几条不同的线路。我很困惑,有些帖子有多个“var”行。再次,非常感谢。
猜你喜欢
  • 2012-03-03
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 2017-05-07
相关资源
最近更新 更多