search
HomeBackend DevelopmentPHP TutorialPHP regular expression to verify whether the input string is in the format of ID number or passport number
PHP regular expression to verify whether the input string is in the format of ID number or passport numberJun 24, 2023 pm 12:11 PM
php regular expressionidentification numberPassport number

ID card number and passport number are common document numbers in people’s lives. When implementing functions involving these document numbers, it is often necessary to perform format verification on the entered numbers to ensure their correctness. In PHP, regular expressions can be used to achieve this function. This article will introduce how to use PHP regular expressions to verify whether the input string is in the format of an ID number or passport number.

1. ID number verification

The ID number is composed of 18 digits and the last digit may be a letter (check code). Its format is as follows:

The first 6 digits are the area code, indicating the place where the ID card is issued;

7~14 digits are the date of birth, of which the 7th to 10th digits represent the year, and the 11th and 12th digits represent The month, the 13th and 14th digits represent the date; the 15th to 17th digits of

are the sequence number, indicating the sequence number of the certificate issued in the region and date; the last digit of

is The check code is mainly used to check whether the first 17 digits comply with national standards.

According to the format of the above ID number, you can use regular expressions for verification. The specific implementation code is as follows:

function validateIDCard($idCard) {
    $pattern = '/^d{6}(19|20)d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]d|3[0-1])d{3}[0-9X]$/';
    if (preg_match($pattern, $idCard)) {
        return true;
    } else {
        return false;
    }
}

In the above code, the preg_match() function and regular expression are used to verify the ID number format. The $pattern variable stores a regular expression used to verify the format of the ID number, where d represents a number, | represents or, and [0-9X] represents that the last digit can be a number or the capital letter X. If the input string conforms to the format of the regular expression, that is, the format of the ID number, then true is returned, otherwise false is returned.

2. Passport number verification

The passport number is composed of numbers and letters, and its format is as follows:

The first to second characters are English characters, indicating the issuance of the passport Country;

The 3rd to 5th characters are numbers or letters, indicating the passport type;

The 6th to 14th characters are numbers, indicating the personal information of the passport;

The last character is a number or letter and is the checksum.

When you need to verify whether the input string conforms to the passport number format, you can also use PHP regular expressions. The specific implementation code is as follows:

function validatePassport($passport) {
    $pattern = '/^[a-zA-Z]{2}d{3}[a-zA-Z0-9]{9}$/';
    if (preg_match($pattern, $passport)) {
        return true;
    } else {
        return false;
    }
}

In the above code, the preg_match() function and regular expression are also used to verify the passport number format. Stored in the $pattern variable is a regular expression used to verify the passport number format, where [a-zA-Z] represents English letters, d represents numbers, and [a-zA-Z0-9] represents both numbers and letters. If the input string conforms to the format of the regular expression, that is, the format of the passport number, then true is returned, otherwise false is returned.

Summary

As can be seen from the above code, it is very convenient to use PHP regular expressions to verify the format of ID number and passport number. Developers can make personalized modifications and optimizations based on application scenarios and needs to meet different verification requirements.

The above is the detailed content of PHP regular expression to verify whether the input string is in the format of ID number or passport number. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
如何使用PHP正则表达式验证输入是否是IPv6地址如何使用PHP正则表达式验证输入是否是IPv6地址Jun 25, 2023 am 09:37 AM

IPv6是指InternetProtocolVersion6,是用于互联网通信的一种IP地址协议。IPv6地址是由128个比特位组成的数字,通常用8个16进制数分组表示。在PHP中,可以使用正则表达式来验证输入是否是IPv6地址,下面就介绍一下如何使用PHP正则表达式验证IPv6地址。第一步:了解IPv6地址的格式IPv6地址由8个16进制块组成,每个

如何使用Python正则表达式进行身份证号码提取如何使用Python正则表达式进行身份证号码提取Jun 22, 2023 am 10:35 AM

在数据处理的过程中,经常需要从文本中提取特定格式的信息。而身份证号码作为一种比较常见的个人信息,在数据处理中也经常被用到。使用Python正则表达式可以方便地提取身份证号码,并且还能对其进行一定的验证。身份证号码是由18位数字组成的,包含了身份证号码中的地区、出生年月日和校验码等信息。在Python中,我们可以使用re模块的正则表达式函数来提取身份证号码。首

如何用PHP正则表达式验证字符串是否为空如何用PHP正则表达式验证字符串是否为空Jun 24, 2023 am 08:46 AM

在PHP中,我们可以使用正则表达式来验证字符串是否为空。字符串为空的情况包括以下情况:字符串只包含空格。字符串长度为0。字符串为null或者未定义。接下来,我们将介绍如何使用PHP中的正则表达式来验证这些情况。正则表达式:s+这个正则表达式可以用来匹配只包含空格的字符串。其中s表示匹配空格,+表示匹配一个或多个。代码示例:functionisEmptySt

如何用PHP正则表达式验证电话号码格式如何用PHP正则表达式验证电话号码格式Jun 24, 2023 am 08:44 AM

在编写Web应用程序时,经常需要进行电话号码的验证。PHP中常用的方法是使用正则表达式来判断电话号码的格式是否正确。正则表达式是一个强大的工具,它可以帮助您在简洁的语句中确定某些模式。下面是在PHP中使用正则表达式来验证电话号码格式的示例。首先,让我们定义电话号码的通用格式。电话号码可以包含数字、括号、连字符和空格。一个标准的电话号码应该包含10个数字,前

如何用PHP正则表达式验证URL地址格式如何用PHP正则表达式验证URL地址格式Jun 24, 2023 am 09:51 AM

随着互联网的快速发展,URL地址已经成为了人们日常生活中不可或缺的一部分。在web开发中,为了保证用户输入的URL地址可以正确地被系统识别和使用,我们需要对其进行格式验证。本文将介绍如何使用PHP正则表达式来验证URL地址格式。一、URL地址的基本组成部分在了解如何验证URL地址格式之前,我们首先需要了解URL地址的基本组成部分。通常,一个标准的URL地址由

PHP正则表达式验证输入字符串是否为身份证号码或护照号码格式PHP正则表达式验证输入字符串是否为身份证号码或护照号码格式Jun 24, 2023 pm 12:11 PM

身份证号码和护照号码是人们生活中常见的证件号码。在实现涉及到这些证件号码的功能时,经常需要对输入的号码进行格式验证,以确保其正确性。而在PHP中,使用正则表达式可以很好地实现这一功能,本文就介绍如何使用PHP正则表达式验证输入字符串是否为身份证号码或护照号码格式。一、身份证号码验证身份证号码是由18位数字和最后一位可能是字母(校验码)组成的,其格式如下:前6

如何在PHP中使用正则表达式验证是否是文件路径如何在PHP中使用正则表达式验证是否是文件路径Jun 24, 2023 am 10:18 AM

在PHP中,正则表达式是一种常用的字符串匹配和验证工具。在开发过程中,需要经常对输入的文件路径进行验证,确保其格式正确。本文将介绍如何使用正则表达式验证一个字符串是否是文件路径。首先,我们需要确定一个文件路径的基本格式。在Windows系统中,一个典型的文件路径是类似于“C:ProgramFilesPHPphp.exe”这样的格式。该路径分为以下几个部分:

如何用PHP正则表达式验证输入字符串是否为正确的身份证号码、护照号码或港澳通行证格式如何用PHP正则表达式验证输入字符串是否为正确的身份证号码、护照号码或港澳通行证格式Jun 24, 2023 am 10:36 AM

身份证、护照和港澳通行证号码都是重要的个人身份证明,为了保障个人信息安全,我们需要在系统中验证用户输入的证件号码是否符合规范格式。而PHP正则表达式是一个非常强大的工具,可以方便地实现这个目的。本文将介绍如何使用PHP正则表达式验证用户输入的身份证号码、护照号码和港澳通行证号码。一、身份证号码格式验证身份证号码是18位数字,在最后一位可能是数字或字母X。身份

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft