JavaScript: using RegExp to check a form

First name: capital letters, no accent, possibly an apostrophe, a hyphen, or a space

    function checkFirstName() {
        document.forms[0].firstname.value = correctCaseAndAccents(document.forms[0].firstname.value);
        var isValid = true;
        var expression = new RegExp(“^[A-Z]+([\\-\\’ ][A-Z]+)*$”);
 
        if (!document.forms[0].firstname.value.match(expression)) {
            isValid = false;
            document.forms[0].firstname.style.backgroundColor = ‘#efcfcd’;
        } else document.forms[0].firstname.style.backgroundColor = ‘white’;
 
        return isValid;
    }



French phone number: 10 digits, possibly a dot, a hyphen, or a space – To be improved


    var expression = new RegExp(“^(0[1-9])([\\-\\. ]?)((\\d\\d)([\\-\\. ]?)){3}(\\d\\d)$”);


According to the regexp, the following strings can be accepted:
0125894748
02 48 11 45 35
03.78.54.25.70


This regexp is to be improved because a mix of the allowed delimiters should not be considered as a match: 04-78 2254.71




Check the date of birth (dd/mm/yyyy): to be improved because of leap years


    function checkDateBirth() {
        var isValid = true;
        var expression = new RegExp(“^(0[1-9]|[12][0-9]|3[01])[\\-\\/](0[1-9]|1[012])[\\-\\/](19|20)\\d\\d$”);

        if (!document.forms[0].dateBirth.value.match(expression)) {
            isValid = false;
            document.forms[0].dateBirth.style.backgroundColor = ‘#efcfcd’;
        } else {
            if(RegExp.$1 == 31 && (RegExp.$2 == 4 || RegExp.$2 == 6 || RegExp.$2 == 9 || RegExp.$2 == 11)) {
                isValid = false;
                document.forms[0].dateBirth.style.backgroundColor = ‘#efcfcd’;
            } else if(RegExp.$1 >= 30 && RegExp.$2 == 2) {
                isValid = false;
                document.forms[0].dateBirth.style.backgroundColor = ‘#efcfcd’;
            }
            else document.forms[0].dateBirth.style.backgroundColor = ‘white’;
        }

        return isValid;
    }


Check the INSEE number (French social security number)



15 digits:
– 1st digit: 1 if male, 2 if female
– 2 last digits: Verification key


    function checkInsee() {
        var isValid = true;    
        var expression = new RegExp(“^[12]\\d{14}$”);
        
        if (!document.forms[0].insee.value.match(expression)) {
            isValid = false;
            document.forms[0].insee.style.backgroundColor = ‘#efcfcd’;
        } else {
            var partialInsee = document.forms[0].insee.value.toString().substring(0, 13);
            var clef = 97 – (parseFloat(partialInsee) % 97);
            if(clef.toString() != document.forms[0].insee.value.toString().substring(13, 15)) {
                isValid = false;
                document.forms[0].insee.style.backgroundColor = ‘#efcfcd’;
            }
            else document.forms[0].insee.style.backgroundColor = ‘white’;
        }

        return isValid;
    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: