当前位置:主页 > 网页前端 > JavaScript代码 >

JS表单数据验证的正则表达式(常用)

时间:2020-12-08 23:06:25 | 栏目:JavaScript代码 | 点击:

常用的验证方法:

checkUserNameflag=false; 
 checkPasswordflag=false; 
 checkPasswordAgianflag=false; 
 checkEmailflag=false; 
// 用户名校验 
function checkUserName() { 
 var username = $("userNeme").value; 
 var zz = /^[A-Za-z0-9]{6,}$/; 
 if (!zz.test(username)) { 
  $("userName_warn").innerHTML = " × 用户名不符合规范"; 
  checkUserNameflag=false; 
 } else { 
  /*$("userName_warn").style.color='blue'; 
  $("userName_warn").innerHTML = "√ 用户名可用";*/ 
  showUserExist(username); 
  checkUserNameflag=true; 
 } 
} 
//重置提示 
function resetWarn(){ 
 $("userName_warn").innerHTML=""; 
 $("email_warn").innerHTML=""; 
 $("password_warn").innerHTML=""; 
 $("Repassword_warn").innerHTML=""; 
} 
// 密码校验 
function checkPassword() { 
 var password1 = $("password").value; 
 if (password1.length >= 6 && password1.length <= 15) { 
  checkPasswordflag = true; 
  $("password_warn").style.color='blue'; 
  $("password_warn").innerHTML = " √ 密码可用 "; 
  checkAgianMiMa(); 
 } else { 
  checkPasswordflag = false; 
  $("password_warn").innerHTML = " × 密码至少为 6 个字符 "; 
 } 
} 
function checkPasswordAgian() { 
 var password1 = $("password").value; 
 var password2 = $("rePassword").value; 
 if (password1 == password2) { 
  if (password1 == "") { 
   $("Repassword_warn").innerHTML = " × 请输入密码 "; 
   checkPasswordAgianflag = false; 
   return; 
  } 
  $("Repassword_warn").style.color='blue'; 
  $("Repassword_warn").innerHTML = "√ 重复输入密码正确"; 
  checkPasswordAgianflag = true; 
 } else { 
  checkPasswordAgianflag = false; 
  $("Repassword_warn").innerHTML = " × 两次密码输入不同 "; 
 } 
} 
function checkEmail(){ 
 //对电子邮件的验证 
  var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; 
  var email=$("email").value; 
  if(!myreg.test(email)){ 
   checkEmailflag=false; 
    $("email_warn").style.color='red'; 
   $("email_warn").innerHTML = " × 邮箱格式不符合规范"; 
  } else { 
   checkEmailflag=true; 
   $("email_warn").style.color='blue'; 
   $("email_warn").innerHTML = "√ 邮箱格式正确"; 
  } 
} 
function submitCheck(){ 
 alert(checkUserNameflag); 
 if(checkUserNameflag==false || checkPasswordflag==false || checkPasswordflag==false 
   ||checkPasswordAgianflag==false || checkEmailflag==false){ 
  return; 
 } 
} 
function $(id){ 
 return document.getElementById(id); 
} 

js正则表达式--验证表单

检测手机号码:/0?(13|14|15|18)[0-9]{9}/

检测用户名:(数字,英文,汉字,下划线,中横线):/^[A-Za-z0-9_\-\u4e00-\u9fa5]+$/

密码:(数字,英文,下划线,中横线)/^[A-Za-z0-9_-]+$/

身份证:/\d{17}[\d|x]|\d{15}/

非空:/^\S+$/

IP地址:/((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))/

您可能感兴趣的文章:

相关文章