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

I am looking through using the Phone Number Validation in the System Policy > Telephone Display Rules > Sys Phone Territory - North America section. However, after applying several regex's in this section, the regexes I apply do not seem to do much. This could be that they are already covered by one of the Phone Validations. Although, looking at those, I am even more confused. The documentation on these is not what I would expect it to be and is not easily understood by me.



Reviewing the phone number validation for US fixedLine with an order of 100 on my Geneva Developer instance, it has the following value in the Condition field:



(?:2(?:0[1-35-9]|1[02-9]|2[4589]|3[149]|4[08]|5[1-46]|6[0279]|7[06]|8[13])|3(?:0[1-57-9]|1[02-9]|2[0135]|3[014679]|47|5[12]|6[01]|8[056])|4(?:0[124-9]|1[02-579]|2[3-5]|3[0245]|4[0235]|58|69|7[0589]|8[04])|5(?:0[1-57-9]|1[0235-8]|20|3[0149]|4[01]|5[19]|6[1-37]|7[013-5]|8[056])|6(?:0[1-35-9]|1[024-9]|2[036]|3[016]|4[16]|5[017]|6[0-279]|78|8[12])|7(?:0[1-46-8]|1[02-9]|2[047]|3[124]|4[07]|5[47]|6[02359]|7[02-59]|8[156])|8(?:0[1-68]|1[02-8]|28|3[0-25]|4[3578]|5[06-9]|6[02-5]|7[028])|9(?:0[1346-9]|1[02-9]|2[0589]|3[1678]|4[0179]|5[1246]|7[0-3589]|8[0459]))[2-9]\d{6}



There is another US fixedLine Phone Number validation with an order of 110 with the following condition field:



[2-9]\d{9}



This might need to be moved into another topic as the original topic was regarding how to do this in JavaScript. I suppose I could use regex in my original JavaScript using the following regex:



^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$



Any help you could provide would be helpful. Thank you again


kevin_eldridge
Kilo Guru

If that is the case, then you are not the only one. This has been out some Dublin.