简体   繁体   English

在客户端验证表单

[英]Validate a form at the client side

I wish to validate a form at the client side (before submit to server). 我希望在客户端(在提交到服务器之前)验证表单。 I have a script to search for special characters (~!@#) etc. If at least one exists, form should not be submitted until the user corrects the error(s). 我有一个脚本来搜索特殊字符(〜!@#)等。如果至少存在一个脚本,则在用户更正错误之前,不应该提交表单。 Here is my form: 这是我的表格:

<form  id="setUp" method="post" action="Usercontact.php">
<table id="contact">
<tbody>
<tr>
<th class="cdtl">Name of user:</td>
<td><input type="text" name='username' required /></td>
</tr>
<tr>
<th class="cdtl">Course</td>
<td><input type="text" name='useraddy1' required  /></td>
</tr>
<tr>
<th class="cdtl">Telephone</td>
<td><input type="text" name='userfone' required  /></td>
</tr>
<tr>
<th class="cdtl">e-mail Address</td>
<td><input type="email" name='schemail' required  /></td>
</tr>
</tbody>
</table>
<br>
<input id="postform" type="submit" onclick="err()" value="Submit form">
</form>

Below is the script 下面是脚本

<script>
function err() {
    var tbl = document.getElementById("contact");
    var name = tbl.rows[0].cells[1].getElementsByTagName("input")[0].value;
    var addy = tbl.rows[1].cells[1].getElementsByTagName("input")[0].value;
    var fone = tbl.rows[2].cells[1].getElementsByTagName("input")[0].value;
    var email = tbl.rows[3].cells[1].getElementsByTagName("input")[0].value;

    var namepos = name.lastIndexOf("~`!@#$%^&*)(+=}{][|\:;'.'/"',>?<");
    var addypos = addy.lastIndexOf("~`!@#$%^&*)(+=}{][|\:;'.'/"',>?<");
    var fonepos = fone.lastIndexOf("~`!@#$%^&*)(+=}{][|\:;'.'/"',>?<");
    var emailpos = name.lastIndexOf("~`!@#$%^&*)(+=}{][|\:;'.'/"',>?<");

    if (namepos !== -1 || addypos !== -1 || fonepos !== -1 || emailpos !== 
    -1) {
        document.getElementById("postform").addEventListener("click",  
        function(event){
        event.preventDefault()
        });
    }
}
</script>

Please why is this script not working. 请为什么这个脚本不起作用。 The form is submitted even when there are special characters in any of the input fields. 即使在任何输入字段中都有特殊字符,也将提交表单。 Appreciate 欣赏

This line: 这行:

var namepos = name.lastIndexOf("~`!@#$%^&*)(+=}{][|\:;'.'/"',>?<");

is checking for the entire string ("~`!@#$%^&*)(+=}{][|:;'.'/"',>?<"), not each character. 正在检查整个字符串(“〜`!@#$%^&*)(+ =} {] [|:;'。'/”',>?<“),而不是每个字符。

As suggested in the comments, you could use the HTML5 pattern attribute: 如注释中所建议,您可以使用HTML5模式属性:

<input name="name" pattern="[A-Za-z]{3,12}">

This would require the name to include only letters and range in length from 3 to 12 characters. 这将要求名称仅包含字母,长度范围为3到12个字符。

To use a regular expression to test for all the characters you listed, you could use: 要使用正则表达式测试列出的所有字符,可以使用:

var re=/[~`!@#$%^&*)(+=}{\]\[|\:;'.'\"',>?<]/
var str="this@that";

if (re.test(str)) {
    console.log('The string has one of the characters');
} else {
    console.log('The string does not have any of the characters');
}

You need to include event. 您需要包括事件。 PreventDefault().. This will prevent the form from submitting if you find characters you are checking for or anything else you might be checking for. PreventDefault()..如果您发现要检查的字符或其他可能要检查的字符,这将阻止表单提交。

Following suggestions above, I have now a working doc as follows: 根据上述建议,我现在有一个工作文档,如下所示:

<form  id="setUp" method="post" action="Usercontact.php">
<table id="contact">
<tbody>
<tr>
<th class="cdtl">Name of user:</td>
<td><input type="text" name='username' required pattern="[A-Za-z0-9.-_]
{5,12}"/></td>
</tr>
<tr>
<th class="cdtl">Course</td>
<td><input type="text" name='useraddy1' required pattern="[A-Za-z0-9]{5,15}" 
/></td>
</tr>
<tr>
<th class="cdtl">Telephone</td>
<td><input type="text" name='userfone' required pattern="[0-9.-_]{6,15}" />     
</td>
</tr>
<tr>
<th class="cdtl">e-mail Address</td>
<td><input type="email" name='schemail' required pattern="[A-Za-z0-9.-_@]" 
/></td>
</tr>
</tbody>
</table>
<br>
<input id='submit' type="submit" value="Submit form">
</form>

The script was removed as there is no need for it. 该脚本已删除,因为不需要它。

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

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