简体   繁体   English

使用JavaScript验证表单

[英]Validating form using javascript

I am trying to validate my form using an external JS file, I am wanting to make "your name", "your email" and "is the new page" a mandatory field which can not be left blank, I have been playing around with different JavaScript functions but just cant seem to get it to work, I feel like It could be a simple mistake I have made but just cant seem to figure it out. 我正在尝试使用外部JS文件验证表单,我想将“您的姓名”,“您的电子邮件”和“是新页面”设为必填字段,不能将其留空,我一直在玩弄不同的JavaScript函数,但似乎无法使其正常工作,我感觉这可能是我犯的一个简单错误,但似乎无法弄清楚。 Would really appreciate it if someone could take a look and see if they can spot my mistake. 如果有人可以看一看是否能发现我的错误,我将不胜感激。 Thanks This is my form: 谢谢这是我的表格:

        <form method="post" action="#">
            <h3 class="formHeader">Form Header</h3>
            <div class="formPod">
                <fieldset role="presentation">
                    <div class="formRow clearfix">
                        <span><label for="customerName">Your Name</label></span>
                        <input name="customerName" id="customerName" type="text" size="60" maxlength="200" />
                    </div>
                    <div class="formRow clearfix">
                        <span><label for="customerEmail">Your Email</label></span>
                        <input name="customerEmail" id="customerEmail" type="text" size="60" maxlength="200" />
                    </div>
                    <div class="formRow clearfix">
                        <fieldset>
                            <legend>Is the new page</legend>
                            <div>
                                <input type="radio" name="radio" id="radioEasier" value="easier" />
                                <span><label for="radioEasier">Easier to use</label></span>
                            </div>
                            <div>
                                <input type="radio" name="radio" id="radioHarder" value="harder" />
                                <span><label for="radioHarder">Harder to use</label></span>
                            </div>
                            <div>
                                <input type="radio" name="radio" id="radioSame" value="same" />
                                <span><label for="radioSame">About the same</label></span>
                            </div>
                        </fieldset>
                    </div>
                    <div class="formRow clearfix">
                        <span>
                            <label for="comments">Comments</label>
                            <label for="comments" id="charsLeft">(300)</label>
                        </span>
                        <textarea name="comments" id="comments" rows="4" cols="40"></textarea>
                    </div>
                </fieldset>
                <div class="actionRow clearfix">
                    <div class="btn">
                        <input type="submit" value="Submit" />
                    </div>
                </div>
            </div>
        </form>

Here is the external JavaScript file I have been playing with to try get the validation to work, starting with the customerName. 这是我一直在使用的外部JavaScript文件,尝试从客户名开始进行验证。

<script type="text/javascript">

function checkForBlank(){
    if (document.getElementById('customerName').value ==""){
        alert('Please enter a name');
        return false:
    }



}

</script>

Thanks. 谢谢。

You consider using HTML attributes? 您考虑使用HTML属性吗? on the input field you can set the required atribute and the field form wont be able to be sent untill its filled. 在输入字段中,您可以设置required属性,并且字段格式只有填满后才能发送。 another option is to use the patter attribute on the markup and use regex to test the validation 另一个选择是在标记上使用patter属性,并使用正则表达式测试验证


http://www.w3schools.com/tags/att_input_required.asp http://www.w3schools.com/tags/att_input_pattern.asp http://www.w3schools.com/tags/att_input_required.asp http://www.w3schools.com/tags/att_input_pattern.asp


Looking at your code, on the js, you need to take a way the ":" after the "false" and replace it with a ";" 在js上查看您的代码,您需要在“ false”之后采用“:”并将其替换为“;”

also in the markup, you never called the function on the form submit thats why you never got the function running. 同样在标记中,您从未在表单提交上调用过函数,这就是为什么您从未使函数运行的原因。 try this: 尝试这个:

HTML 的HTML

<form method="post" action="#">
    <h3 class="formHeader">Form Header</h3>
    <div class="formPod">
        <fieldset role="presentation">
            <div class="formRow clearfix">
                <span><label for="customerName">Your Name</label></span>
                <input name="customerName" id="customerName" type="text" size="60" maxlength="200" />
            </div>
            <div class="formRow clearfix">
                <span><label for="customerEmail">Your Email</label></span>
                <input name="customerEmail" id="customerEmail" type="text" size="60" maxlength="200" />
            </div>
            <div class="formRow clearfix">
                <fieldset>
                    <legend>Is the new page</legend>
                    <div>
                        <input type="radio" name="radio" id="radioEasier" value="easier" />
                        <span><label for="radioEasier">Easier to use</label></span>
                    </div>
                    <div>
                        <input type="radio" name="radio" id="radioHarder" value="harder" />
                        <span><label for="radioHarder">Harder to use</label></span>
                    </div>
                    <div>
                        <input type="radio" name="radio" id="radioSame" value="same" />
                        <span><label for="radioSame">About the same</label></span>
                    </div>
                </fieldset>
            </div>
            <div class="formRow clearfix">
                <span>
                    <label for="comments">Comments</label>
                    <label for="comments" id="charsLeft">(300)</label>
                </span>
                <textarea name="comments" id="comments" rows="4" cols="40"></textarea>
            </div>
        </fieldset>
        <div class="actionRow clearfix">
            <div class="btn">
                <input type="submit" value="Submit" onclick="checkForBlank();" />
            </div>
        </div>
    </div>

js js

<script type="text/javascript">
    function checkForBlank(){
        if (document.getElementById('customerName').value === ""){
            alert('Please enter a name');
            return false;
        }
    }
</script>

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

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