欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javascript进行表单验证

程序员文章站 2022-03-01 14:51:38
...
<!DOCTYPE html>
<html>
<head>
	<style type="text/css"> 
  	 .error{ color: red; }
     .success{color:green;}
	</style>
	<title></title>
  <script type="text/javascript"> 
 function checkForm(){ 
  var nametip = checkUserName(); 
  var passtip = checkPassword();  
  var conpasstip = ConfirmPassword(); 
  var emailtip = checkEmail(); 
  return nametip && passtip && conpasstip && emailtip; 
  } 
  //验证用户名   
  function checkUserName(){ 
  var username = document.getElementById('userName'); 
  var errname = document.getElementById('nameErr'); 
  var pattern = /^\w{3,}$/;  //用户名格式正则表达式:用户名要至少三位 
  if(username.value.length == 0){ 
    errname.innerHTML="用户名不能为空"
    errname.className="error"
    return false; 
    } 
  if(!pattern.test(username.value)){ 
    errname.innerHTML="用户名不合规范"
    errname.className="error"
    return false; 
    } 
   else{ 
     errname.innerHTML="OK"
     errname.className="success"; 
     return true; 
     } 
  } 
  //验证密码   
function checkPassword(){ 
  var userpasswd = document.getElementById('userPasword'); 
  var errPasswd = document.getElementById('passwordErr'); 
  var pattern = /^\w{4,8}$/; //密码要在4-8位 
  if(!pattern.test(userpasswd.value)){ 
    errPasswd.innerHTML="密码不合规范"
    errPasswd.className="error"
    return false; 
    } 
   else{ 
     errPasswd.innerHTML="OK"
     errPasswd.className="success"; 
     return true; 
     } 
  } 
  //确认密码 
  function ConfirmPassword(){ 
  var userpasswd = document.getElementById('userPasword'); 
  var userConPassword = document.getElementById('userConfirmPasword'); 
  var errConPasswd = document.getElementById('conPasswordErr'); 
  if((userpasswd.value)!=(userConPassword.value) || userConPassword.value.length == 0){ 
    errConPasswd.innerHTML="上下密码不一致"
    errConPasswd.className="error"
    return false; 
    } 
   else{ 
     errConPasswd.innerHTML="OK"
     errConPasswd.className="success"; 
     return true; 
     }    
  } 
//验证手机号 
  function checkEmail(){ 
  var useremail= document.getElementById('userEmail'); 
  var emailErr = document.getElementById('emailErr'); 
  var pattern =  /[~#^[email protected]%&!*()<>:;'"{}【】  ]/; //验证手机号正则表达式 
  if(!pattern.test(useremail.value)){ 
    emailErr.innerHTML="邮箱不合规范"
    emailErr.className="error"
    return false; 
    } 
   else{ 
     emailErr.innerHTML="OK"
     emailErr.className="success"; 
     return true; 
     } 
  } 
</script>

</head>
<body>
 <div class="divAll"> 
   <div id="contents"> 
   <hr width="95%" color="#f2f2f2"/> 
   <form action="#" onSubmit="return checkForm()"> 
    <div id="form-itemGroup"> 
      <label for="userName">用户名:</label> 
      <input type="text" id="userName" class="userName" onBlur="checkUserName()" oninput="checkUserName()"> 
      <span class="default" id="nameErr">请输入至少3位的用户名</span> 
    </div> 
    <div id="form-itemGroup"> 
      <label for="userPasword">密码:</label> 
      <input type="password" id="userPasword" class="userName" onBlur="checkPassword()" oninput="checkPassword()"> 
      <span class="default" id="passwordErr">请输入4到8位的密码</span> 
    </div> 
    <div id="form-itemGroup"> 
      <label for="userConfirmPasword">确认密码:</label> 
      <input type="password" id="userConfirmPasword" class="userName" onBlur="ConfirmPassword()" oninput="ConfirmPassword()"> 
      <span class="default" id="conPasswordErr">请再输入一遍密码</span> 
    </div> 
    <div id="form-itemGroup"> 
      <label for="userEmail">邮箱:</label> 
      <input type="text" id="userEmail" class="userName" onBlur="checkEmail()" oninput="checkEmail()"> 
      <span class="default" id="emailErr">请输入正确的邮箱格式</span> 
    </div> 
    <div> 
     <button type="submit" class="divBtn">注册</button> 
    </div> 
   </form> 
   </div> 
 </div>

</body>
</html>

运行结果:

javascript进行表单验证

javascript进行表单验证