- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2016 11:54 AM
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
Solved! Go to Solution.
- Labels:
-
Service Mapping

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2016 02:15 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2016 11:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2016 12:13 PM
Hello Pradeep,
Thank you for your response. That will probably answer my ###-###-#### portion of the two questions I asked above. However, for the other portion for the x#### or X####, what would you suggest I use? My code is listed below that I am currently attempting to use and not having much luck with:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
if (newValue == oldValue) {
return;
}
// Pattern for the regex comparison of the value entered into the "Phone number extension" field
// uses the format x#### or X####
if (newValue) {
if (newValue != oldValue) {
var phoneTempVar = newValue;
if (phoneTempVar.length == 5) {
phoneTempVar = phoneTempVar.replace(/[^\d]+/g, '');
.replace(/^[xX]\d{4}$/, '$1$2');
g_form.setValue('u_phone_number_extension', phoneTempVar);
}
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2016 12:12 PM
Hi Kevin,
Here are a couple of client scripts I've used together to validate phone number entry (basic 10-digit, area code + number, etc.). It's a little more forgiving, as it strips extraneous formatting and typos to return just the numeric part of the entry, and removes any leading "1" that is entered as well. You can include it and have it called from an onChange client script if you like using the checkPhoneNumber(...) function. Notice that the "var check =..." statement sets the number of digits (e.g., 10) used to determine if the entry is a valid number, you could adjust this to use for both your phone numbers and extensions using the numDigits parameter that gets passed into checkPhoneNumber(...):
function checkPhoneNumber(fieldName, control, oldValue, newValue, isLoading, numDigits) {
if (isLoading || newValue == '' || newValue == oldValue || control.getAttribute('validated') == 'true') {
control.setAttribute('validated',"false");
return;
}
try{
g_form.hideFieldMsg(fieldName, true);
var check = isValidPhone(newValue, numDigits);
if(check.isValid){
control.setAttribute('validated',"true");
g_form.setValue(fieldName, check.number);
return;
}
g_form.showFieldMsg(fieldName,"Not a Valid Phone Number");
g_form.setValue(fieldName, oldValue);
}
catch(e){
g_form.addErrorMessage("Error: " + e);
}
}
function isValidPhone(value, reqLength){
var result = {};
var stripped = value.replace(/[\D]/gi, '').replace(/^1/, '');
result.number = ((result.isValid = stripped.length == reqLength)== true) ? stripped:'';
return result;
}
See if that will work for you. Otherwise, you could use more explicit regex statements if you really wanted to match exactly on "###-###-####" and "x####"/"X####".
Thanks,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2016 12:59 PM
Hello Brian,
That is exactly what I am attempting to perform, use more explicit regex statements to capture, "###-###-####" on a Phone Number field and "x####" or "X####" on a Phone Number Extension field. I greatly appreciate the regex statement you provided and am looking for how to capture those values using the onChange function or another variation as you provided.
Thank you for your assistance,
Kevin