Skip to content
PHP Logo

Are there best practices or recommended resources for validating personal identification numbers in PHP?

Validating personal identification numbers (such as social security numbers or driver's license numbers) in PHP can be done using regular expressions to ensure the input matches the expected format. It's important to validate these numbers to prevent errors and ensure data integrity.

php
// Regular expression to validate a social security number (SSN) in the format XXX-XX-XXXX
$ssn_pattern = "/^\d{3}-\d{2}-\d{4}$/";

// Function to validate a social security number
function validateSSN($ssn) {
    global $ssn_pattern;
    if(preg_match($ssn_pattern, $ssn)) {
        return true;
    } else {
        return false;
    }
}

// Example of validating a social security number
$ssn = "123-45-6789";
if(validateSSN($ssn)) {
    echo "Valid SSN";
} else {
    echo "Invalid SSN";
}