mardi 19 décembre 2017

Regex using Javascript

I am trying to create a function that would remove certain characters from a phone number field. So far I have removed parenthesis, spaces, and hyphens as in "(321) 321-4321". Also I removed dots as in "321.321.4321". Lastly I have validated when 11 digits and starting with 1 even with punctuation.

The code I input:

 function phoneNumber(){
      var number = '(321) 321-4321';
      number = number.replace(/[^\d]/g, '');
     return number;
    }

I am now trying to have it return null for certain situations like: It should return null when more than 11 digits,should return null when letters and/or punctuation are present,should return null when area code does not start with 2-9 should return null when exchange code does not start with 2-9.

Any attempt to add a statement invalidates my original function. Can anyone help me I am new to regex, but I need help.

Aucun commentaire:

Enregistrer un commentaire