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.
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:
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; }
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.
The passport number is a string of numbers and letters, usually 6 to 9 digits. The structure of the passport number is as follows:
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; }
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.
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:
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; }
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!