How do I create a regex that only allows extensions and not full phone numbers?

kevin_eldridge
Kilo Guru

I am attempting to create two Client Scripts that uses regex to validate the information being entered. We want to use this for the following two fields:

Phone Number (###-###-####)

Phone Number Extension x#### or X####)

Can someone please assist me in creating the regex I need to use in the client script to write this validation?

I have searched and found several references on RegEx, such as JavaScript RegExp Reference How to validate a string field for proper phone number format

However, I must not understand fully how RegEx is supposed to work to validate the data being entered as my code keeps returning invalid.

Thank you,

Kevin

1 ACCEPTED SOLUTION

Hi Kevin,



You might first look here for a quick tutorial on using the method string.match() with regex: JavaScript String match() Method



The match() method returns an array of all matches found, in your case you want there to only be one (and only one).   The trick then is getting your regex expression right for your requirements.



Here is a great RegEx reference sheet:   Regular Expressions - Dzone Refcardz





Now, down to brass tacks... here are a couple of examples on how to use regex and match() for your purposes:



function validatePhoneNumber(phoneValue){


var strPhMatches = phoneValue.match(/^\d{3}-\d{3}-\d{4}$/);


if(strPhMatches!==null){


return true;


}


else{


alert("Invalid Phone Number");


return false;


}


}



function validateExtension(extValue){


var strExtMatches = extValue.match(/^x\d{4}$/i);


if(strExtMatches!==null){


var phoneTempVar = strExtMatches[0].replace(/x/i, '');


g_form.setValue('u_phone_number_extension', phoneTempVar);


return true;


}


else{


g_form.clearValue('u_phone_number_extension');


alert("Invalid Extension");


return false;


}


}






Thanks,


-Brian


View solution in original post

11 REPLIES 11

Hi Kevin,



You might first look here for a quick tutorial on using the method string.match() with regex: JavaScript String match() Method



The match() method returns an array of all matches found, in your case you want there to only be one (and only one).   The trick then is getting your regex expression right for your requirements.



Here is a great RegEx reference sheet:   Regular Expressions - Dzone Refcardz





Now, down to brass tacks... here are a couple of examples on how to use regex and match() for your purposes:



function validatePhoneNumber(phoneValue){


var strPhMatches = phoneValue.match(/^\d{3}-\d{3}-\d{4}$/);


if(strPhMatches!==null){


return true;


}


else{


alert("Invalid Phone Number");


return false;


}


}



function validateExtension(extValue){


var strExtMatches = extValue.match(/^x\d{4}$/i);


if(strExtMatches!==null){


var phoneTempVar = strExtMatches[0].replace(/x/i, '');


g_form.setValue('u_phone_number_extension', phoneTempVar);


return true;


}


else{


g_form.clearValue('u_phone_number_extension');


alert("Invalid Extension");


return false;


}


}






Thanks,


-Brian


Hello Brian,



Your previous information was helpful. This post is extremely helpful. I will review it and the sites you linked to and come up with a solution that works for what we need.



Thank you to you and everyone else for your assistance,



Kevin Eldridge


Hi Kevin,



Just a note, I used "alert" just for demo purposes.   You might want to use something else (e.g., g_form.addInfoMessage(..)) to include client notices.




Thanks,


-Brian


I did not realize the Phone Number formats section of ServiceNow allows you to specify regex's as a Pattern as well as the format, $1-$2, ($1) $2-$3, etc, and the order in which they are initialized. I think this is the route I will go down as opposed to specifying a scripted approach to this.



Phone validations


Phone formats


Neither did I, thanks.       The validation was something I tackled early on and haven't gone back to look over.   Seems as though I may have re-invented some sort of circular device...




-Brian