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

How to use regular expressions to verify the registry

php中世界最好的语言
Release: 2018-03-29 14:17:26
Original
2035 people have browsed it

This time I will bring you the use of regular expressions to verify the registry and the precautionsof using regular expressions to verify the registry. The following are practical cases. , let’s take a look.

Regular expression

Regular expression is a logical formula for stringoperation, which is to use prior Some defined specific characters and the combination of these specific characters 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 "matching") ;

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 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

Remarks :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.Check 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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use regular expressions to group strings

Use regular expressions to verify the input content of the login page

The above is the detailed content of How to use regular expressions to verify the registry. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!