Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
774 B
5
Indexable
Never
function validateItalianPhoneNumber(phoneNumber) {
    // Remove any non-digit characters from the phone number
    phoneNumber = phoneNumber.replace(/\D/g, '');

    // Check if the number starts with "+39" and transform it to "0039"
    if (phoneNumber.startsWith('+39')) {
        phoneNumber = '0039' + phoneNumber.slice(3);
    }

    // Check if the number starts with "0039"
    if (!phoneNumber.startsWith('0039')) {
        // If not, add "0039" to the beginning
        phoneNumber = '0039' + phoneNumber;
    }

    // Check if the resulting number is a valid Italian phone number
    if (/^0039\d{10}$/.test(phoneNumber)) {
        return true; // Valid Italian phone number
    } else {
        return false; // Invalid phone number
    }
}
Leave a Comment