JavaScript 正则表达式验证登录实例

版权声明:ByRisonBoy https://blog.csdn.net/Rison_Li/article/details/82960172

代码片段:


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>qq登录模拟测试</title>
</head>
<body>
<form name="form1" action="#" method="post">
    <input type="text" placeholder="账号" name="username" ><br><br>
    <input type="password" placeholder="密码" name="password" id="pw">
	<input type="button" name="" id="btn" value="点击显示" />
	<br><br>
    <input name="Submit" type="button"  onClick="check()" value="登录"/>
</form>
</body>
<!--账号密码验证-->
<script type="text/javascript">
function check(){
/*非空*/
if(form1.username.value==""){
alert("账号不能为空");
form1.username.focus();
return false;
}

/*含非数字、首字母为0 /^[0-9]*$/*/
var number=/^(0|[1-9][0-9]*)$/; 
var objExp1=new RegExp(number); 
if(objExp1.test(form1.username.value)==false){
alert("账号不能含非数字和首字符为0");
form1.username.focus();
return false;
}

/*账号小于6位数*/
if(form1.username.value.length < 6){
alert("账号位数过小,请输入6-11位数字");
form1.username.focus();
return false;
}

/*账号大于11位数*/
if(form1.username.value.length > 11){
alert("账号位数过大,请输入6-11位数字");
form1.username.focus();
return false;
}

/*密码非空*/
if(form1.password.value==""){
alert("密码不能为空");
form1.password.focus();
return false;
}

/*密码含有非符号、字母、数字以外字*/
var illegal=/^[0-9a-zA-Z!~@#$%^&*()_+`\-={}:";'<>?,.\/]*$/g; 
var objExp2=new RegExp(illegal);
if(objExp2.test(form1.password.value)==false){
alert("密码含有非法字符");
form1.password.focus();
return false;
}

/*密码小于6位数*/
if(form1.password.value.length < 6){
alert("密码位数过小,请输入6-16位数字");
form1.password.focus();
return false;
}

/*密码大于16位数*/
if(form1.password.value.length > 16){
alert("密码位数过大,请输入6-16位数字");
form1.password.focus();
return false;
}

form1.submit();
}
</script>

<!--回显密码-->
<script type="text/javascript">
    window.onload=function(){
    var btn=document.getElementById("btn");
    var password=document.getElementById("pw")
    btn.onmousedown=function(){
        password.type="text"
     };
    btn.onmouseup=btn.onmouseout=function(){
        password.type="password"
     }
 }
 </script>
</html>

结果显示:

希望对你学习有帮助。

猜你喜欢

转载自blog.csdn.net/Rison_Li/article/details/82960172
今日推荐