常用JS表单验证汇总(持续更新)

首先是测试html页面的编写

1
2
3
4
5
6
7
8
<form name="form1" action="#" method="post" onsubmit="return check()">

帐号:<input name="ID" type="text" />
密码:<input name="PWD" type="password" />
<input name="submit" type="submit" value="提交" />  
<input name="reset" type="reset" value="取消" />

</form>

以下为相应功能的js脚本
①验证为非空

1
2
3
4
5
6
7
8
9
10
function check()
{
    if(document.form1.ID.value=="" || document.form1.PWD.value=="" )
    {
        alert("用户名或密码不能为空!");
        this.form1.ID.focus();
        return false;
    }
else form1.submit();
}

②密码位数验证

1
2
3
4
5
6
7
//添加进check()中即可
if(document.form1.zhanghao.value.length<6)
{
    alert("用户名字符不能少于6位!");
    this.form1.zhanghao.focus();
    return false;
}

BeiTown
2013.06.07

本文链接:常用JS表单验证汇总(持续更新)

转载声明:BeiTown原创,转载请注明来源:BeiTown's Coder 编码之源,谢谢


Tags: , , ,

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>