Home Backend Development PHP Tutorial Share regular expression registry verification and some commonly used verification examples

Share regular expression registry verification and some commonly used verification examples

Jul 16, 2017 pm 01:42 PM
Registry expression verify

正则表达式看起来好像是一大推符号组成的,让人很是头大,我想信很多人刚来时看的时候头疼,我也是的。但是不学又不行,这个是很重要的东西,今天就在这里个大家总结了一些分享正则表达式注册表验证和一些常用的校验实例。

正则表达式

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。

正则表达式目的

1. 给定的字符串是否符合正则表达式的过滤逻辑(称作“匹配”);

2. 可以通过正则表达式,从字符串中获取我们想要的特定部分。

正则表达式的特点是

1.灵活性、逻辑性和功能性非常的强;

2.可以迅速地用极简单的方式达到字符串的复杂控制;

3.对于刚接触的人来说,比较晦涩难懂。

吾尝终日而思矣 不如须臾之所学也,所以学习这个要持之以恒。

注册表验证

1.获取id

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

2.验证姓名


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

备注:s1为判断提示内容,添加在输入框后

3.验证密码


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

4.验证邮箱


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

5.验证手机号


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

6.验证身份证号

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

7.验证QQ号

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.检测所有条件

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

常用的正则表达式校验

  1. 校验密码强度

密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间。

/^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/
Copy after login

2. 校验中文

字符串仅能是中文。

/^[\\u4e00-\\u9fa5]{0,}$/
Copy after login

3. 由数字、26个英文字母或下划线组成的字符串

/^\\w+$/
Copy after login

4. 校验E-Mail 地址

同密码一样,下面是E-mail地址合规性的正则检查语句。

/[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?/
Copy after login

5. 校验身份证号码

下面是身份证号码的正则校验。15 或 18位。

15位:

/^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$/
Copy after login

18位:

 /^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$/
Copy after login

6. 校验日期

“yyyy-mm-dd“格式的日期校验,已考虑平闰年。

 /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/
Copy after login

7. 校验金额

金额校验,精确到2位小数。

/^[0-9]+(.[0-9]{2})?$/
Copy after login

8. 校验手机号

下面是国内 13、15、18开头的手机号正则表达式。(可根据目前国内收集号扩展前两位开头号码)

/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$/
Copy after login

9. 判断IE的版本

IE目前还没被完全取代,很多页面还是需要做版本兼容,下面是IE版本检查的表达式。

/ ^.*MSIE[5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$/
Copy after login

10. 校验IP-v4地址

IP4 正则语句。

/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b/
Copy after login

The above is the detailed content of Share regular expression registry verification and some commonly used verification examples. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Fix: WD My Cloud doesn't show up on the network in Windows 11 Fix: WD My Cloud doesn't show up on the network in Windows 11 Oct 02, 2023 pm 11:21 PM

If WDMyCloud is not showing up on the network in Windows 11, this can be a big problem, especially if you store backups or other important files in it. This can be a big problem for users who frequently need to access network storage, so in today's guide, we'll show you how to fix this problem permanently. Why doesn't WDMyCloud show up on Windows 11 network? Your MyCloud device, network adapter, or internet connection is not configured correctly. The SMB function is not installed on the computer. A temporary glitch in Winsock can sometimes cause this problem. What should I do if my cloud doesn't show up on the network? Before we start fixing the problem, you can perform some preliminary checks:

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

How to improve computer startup speed by modifying Windows 10 registry How to improve computer startup speed by modifying Windows 10 registry Apr 19, 2024 am 08:01 AM

In the process of using the Windows 10 operating system, many users find that it takes a long time to start up their computers, which affects the user experience. In order to optimize the boot speed, some people have suggested that the purpose can be achieved by modifying the system registry. In response to this demand, this article will provide an in-depth analysis of how to effectively improve computer startup speed by properly adjusting Windows 10 registry settings. Let’s take a look below. Operation method 1. Use the "win+R" shortcut key to start running, enter "regedit", and click "OK" to open. 2. After entering the Registry Editor, click "HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurren" in the left column

Detailed method to unblock using WeChat friend-assisted verification Detailed method to unblock using WeChat friend-assisted verification Mar 25, 2024 pm 01:26 PM

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

How to verify whether the input is full-width characters in golang How to verify whether the input is full-width characters in golang Jun 25, 2023 pm 02:03 PM

In golang, Unicode encoding and rune type are required to verify whether the input is full-width characters. Unicode encoding is a character encoding standard that assigns a unique numeric code point to each character in the character set, which includes full-width characters and half-width characters. The rune type is the type used to represent Unicode characters in golang. The first step is to convert the input into a rune type slice. This can be converted by using golang's []rune type, e.g.

How to validate IFSC code using regular expressions? How to validate IFSC code using regular expressions? Aug 26, 2023 pm 10:17 PM

Indian Financial System Code is the abbreviation. Indian bank branches participating in the electronic funds transfer system are identified by a special 11-character code. The Reserve Bank of India uses this code in internet transactions to transfer funds between banks. IFSC code is divided into two parts. Banks are identified by the first four characters, while branches are identified by the last six characters. NEFT (National Electronic Funds Transfer), RTGS (Real Time Gross Settlement) and IMPS (Immediate Payment Service) are some of the electronic transactions that require IFSC codes. Method Some common ways to validate IFSC codes using regular expressions are: Check if the length is correct. Check the first four characters. Checkthefifthcharacter.Che

New features in PHP 8: Added verification and signing New features in PHP 8: Added verification and signing Mar 27, 2024 am 08:21 AM

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

How to adjust UAC settings for standard user accounts in Windows 11 via Registry Editor How to adjust UAC settings for standard user accounts in Windows 11 via Registry Editor Apr 17, 2024 pm 03:58 PM

In the Windows 11 operating system, User Account Control (UAC) serves as a security mechanism that can effectively prevent unauthorized system changes and prompt potential risks. For those who want to adjust the UAC behavior of standard user accounts through deeper settings, this article will explain in detail how to use the Registry Editor tool to make corresponding settings changes in Windows 11 systems. Operation method 1. Right-click "Start" in the taskbar in the lower right corner and select "Run" in the option list. 2. Enter "regedit" in the pop-up run window and press Enter to open it. 3. Then click "Yes" in the prompt window given. 4. After entering the Registry Editor, click "HKEY_" in the left column

See all articles