Home  >  Article  >  Backend Development  >  PHP 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 number

PHPz
PHPzOriginal
2023-06-24 12:11:581904browse

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