Home > Backend Development > PHP Tutorial > PHP regular expression to verify whether the input string is the correct ID card, passport or driver's license number format

PHP regular expression to verify whether the input string is the correct ID card, passport or driver's license number format

PHPz
Release: 2023-06-24 18:04:01
Original
1453 people have browsed it

In many websites, users need to enter some personal information, such as ID number, passport number, driver's license number, etc. It is very necessary to perform format verification on this information to prevent users from entering incorrect data and affecting the accuracy of the data. This article will introduce how to use PHP regular expressions to verify whether the input string is the correct ID card, passport or driver's license number format.

  1. Verify ID number

The ID number is a string of 18 digits or a combination of numbers and letters, where the last digit may be a number or the letter X. The structure of the ID number is as follows:

  • The first 6 digits are the address code;
  • The next 8 digits are the date of birth code;
  • The last 4 digits It's a sequence code.

Use regular expressions to verify the format of the ID number, you can use the following code:

function checkIDCard($id)
{
    if (!preg_match("/^d{17}[d|x]$|^d{15}$/i", $id)) {
        return false;
    }
    return true;
}
Copy after login

First, this function uses the preg_match function to match the regular expression. The "|" (option) operator is used in the regular expression, indicating that there are two verification methods. In the first way, the ID number is an 18-digit number or a combination of numbers and letters, and the last digit may be a number or the letter X. In the second way, the ID number is 15 digits.

  1. Verify passport number

The passport number is a string of numbers and letters, usually 6 to 9 digits. The structure of the passport number is as follows:

  • The first two digits are the issuing country code, usually an English abbreviation;
  • The middle digit is the personal identification code;
  • The last The number is the check code.

Use regular expressions to verify the format of the passport number. You can use the following code:

function checkPassport($passport)
{
    if (!preg_match('/^[A-Za-z0-9]{6,9}$/', $passport)) {
        return false;
    }
    return true;
}
Copy after login

This function uses the preg_match function to match the regular expression. The "[]" (character set) operator is used in regular expressions to match any one of the characters. Only numbers and uppercase and lowercase letters are matched here, adding up to 6 to 9 digits.

  1. Verify driver's license number

The driver's license number is a string of numbers and letters, usually 12 digits. The structure of the driver's license number is as follows:

  • The first digit is a letter; the middle 4 digits of
  • are numbers; the last 7 digits of
  • are a combination of numbers or letters.

Use regular expressions to verify the format of the driver's license number. You can use the following code:

function checkDriverLicense($license)
{
    if (!preg_match('/^[A-Za-z][0-9]{4}[A-Za-z0-9]{7}$/', $license)) {
        return false;
    }
    return true;
}
Copy after login

This function uses the preg_match function to match the regular expression. The regular expression first matches a letter, then 4 numbers, and finally matches 7 numbers or letter combinations.

Summary

The above is how to use PHP regular expressions to verify the format of ID card, passport and driver's license numbers. When performing regular expression verification, pay attention to the syntax of the regular expression and the verification type. If used improperly, data may not be properly validated, resulting in data inaccuracy.

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

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