Home > Web Front-end > JS Tutorial > body text

Regular expression for JS form data validation

高洛峰
Release: 2017-02-20 16:41:05
Original
1294 people have browsed it

This article mainly introduces the regular expressions for JS form data verification. This method is more commonly used, as well as the method of using regular expressions to verify the form. This article introduces you in very detail. Friends who need it can refer to it

Commonly used verification methods:


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=&#39;blue&#39;; 
  $("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=&#39;blue&#39;; 
  $("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=&#39;red&#39;; 
   $("email_warn").innerHTML = " × 邮箱格式不符合规范"; 
  } else { 
   checkEmailflag=true; 
   $("email_warn").style.color=&#39;blue&#39;; 
   $("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); 
}
Copy after login


js regular expression--validation form

Detect mobile phone number:/0?(13|14|15|18)[0-9]{9}/

Detect user name: (numbers, English, Chinese characters, underline, dash): /^[A-Za-z0-9_\-\u4e00-\u9fa5]+$/

Password: ( Numbers, English, underline, dash)/^[A-Za-z0-9_-]+$/

ID card:/\d{17}[\d|x]|\d{ 15}/

Not empty:/^\S+$/

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

The above is the regular expression for JS form data verification introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to regular expressions for JS form data validation, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template