Home > Backend Development > PHP Tutorial > Organizing regular expression registry verification notes

Organizing regular expression registry verification notes

小云云
Release: 2023-03-17 19:26:01
Original
1365 people have browsed it

Regular expression is a logical formula for string operations. It uses some predefined specific characters and combinations of these specific characters to form a "rule string". This "rule string" is used to express A filtering logic for strings.

Purpose of regular expression

1. Whether the given string conforms to the filtering logic of the regular expression (called " Match");

2. We can get the specific part we want from the string through regular expressions.

The characteristics of regular expressions are

1. Flexibility, logic and functionality are very strong;

2. It can quickly achieve complex control of strings in an extremely simple way;

3. It is relatively obscure for those who are new to it.

Registry verification

1. Get the id

function $(id){
  return document.getElementById(id);
}
Copy after login

2. Verify name

function checkName(){
  //获取值
  var username=$('user').value;
  //判断不能为空
  if(username==''){
    $('s1').innerHTML='用户名不能为空';
    return false;
  }
  //正则表达式
  var reg=/^[a-zA-Z][a-zA-Z0-9]{4,9}$/;
  //检测输入内容是否匹配正则表达式
  if(!reg.test(username)){
    $('s1').innerHTML='用户名必须是5-10位数字或字母组成,开头不能是数字';
    return false;
  }
  //匹配,返回空
    $('s1').innerHTML='';
    return true;
}
Copy after login

Note: s1 is the judgment prompt content, added after the input box

3.Verify password

function checkPwd(){
  //获得值
    同上…
  //判断不能为空
    同上…
  //正则表达式
  var reg=/^\S{6,}$/;
  //检测输入内容是否匹配正则表达式
  if(reg.test(password)==false){
    $('s2').innerHTML='密码必须是6位以上';
    return false;
  }
  //匹配,返回空
    同上…
}
Copy after login

4.Verify email

function checkEmail(){
  //获得值
    同上…
  //判断不能为空
    同上…
  //正则表达式
  var reg=/^\w+@\w+\.com|cn|net$/;      
  //检测输入内容是否匹配正则表达式
  if(!reg.test(email)){
    $('s3').innerHTML='邮箱不合法';
    return false;
  }
  //匹配,返回空
    同上…
}
Copy after login

5. Verify mobile phone number

function checkTel(){
  //获得值
    同上…
  //判断不能为空
    同上…
  //正则表达式
  var reg=/^1[34578]\d{9}$/;     
  //检测输入内容是否匹配正则表达式
  if(!reg.test(tel)){
    $('s4').innerHTML='手机号码不合法';
    return false;
  }
  //匹配,返回空
    同上…
}
Copy after login

6.Verify ID number

function checkCid(){
  //获得值
    同上…
  //判断不能为空
    同上…
  //正则表达式
  var reg=/^\d{15}$|^\d{17}\d|x$/;     
  //检测输入内容是否匹配正则表达式
  if(!reg.test(cid)){
    $('s5').innerHTML='身份证不合法';
    return false;
  }
  //匹配,返回空
    同上…
}
Copy after login

7. Verify QQ number

function checkQQ(){
  //获得值
    同上…
  //判断不能为空
    同上…
  //正则表达式
  var reg=/^[1-9]\d{7,10}$/;     
  //检测输入内容是否匹配正则表达式
  if(!reg.test(qq)){
    $('s6').innerHTML='QQ必须是8到11数字组成,开头不能是0';
    return false;
      }
  //匹配,返回空
    同上…
}
Copy after login

8. Detect all conditions

function checkAll(){
  if(checkName()&&checkPwd()&&checkEmail()&&checkTel()&&checkCid()&&checkQQ()){
    return true;
  }else{
    return false;
  }
}
Copy after login

Of course, in the later stage, you can use the jQuery Validate verification framework for expression verification, which is more convenient.

The above content is a compilation of regular expression registry verification notes. I hope it can help everyone.

Related recommendations:

The most complete PHP regular expression in history_regular expression

The concept and use of regular expressions in JavaScript Application_regular expression

PHP regular expression collection

The above is the detailed content of Organizing regular expression registry verification notes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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