简单表单验证js

来源:互联网 发布:淘宝退货运费险退多少 编辑:程序博客网 时间:2024/05/18 01:10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form  action="1.php" method="post" name="form">
用户名<input type="text" name="user"><br>
密码<input type="password" name="password"><br>
<input type="button" value='提交' onclick="check()">
</form>
<script>
function  check(){
var form=document.form;
var  value=document.form.user.value;
var pwd=document.form.password.value;
console.log(value);
if(value=='')
{
alert("用户名不能为空")
form.reset();
}
else if(charcheck(value))
{
alert("包含特殊字符")
form.reset();
}
else if(value.length<6||value.length>18)
{
alert('用户名长度不能小于6,大于8')
form.reset();
}
else if(pwd==""){
alert("密码不能为空")
form.reset();
}
else if(pwd.length<8||pwd.length>16){
alert("密码不能小于8位大于16位")
form.reset();
}


else{
alert('用户名合法')
form.submit();
}


}
function charcheck(str){
var arr=["*","&","<",">","$","\\","/","@"];
for(var i=0;i<arr.length;i++)
{
for(var j=0;j<str.length;j++)
{
if(arr[i]==str.charAt(j))
{return true;}


}
}
return false;
}
</script>
</body>
</html>
原创粉丝点击