Phone Number formatting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2016 06:43 AM
Hi,
Can you help me out.I have a requirement where the user entered phone number should convert to one particular format((XXX)XXX-XXXX) even if the user enters in different formats like XXXXXXXXXX,XXX-XXX-XXXX,(XXX)XXX-XXXX.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2016 07:02 AM
Hi Santhu,
Whatever format the user will enter, it will probably have only 10 numbers. So keeping that in mind, from the format that the user has entered try to get the 10 digits using the NAN operator.
Once you have the 10 digits and in order, you can populate them in your desired format.
You can try and let me know.
Thanks and Regards,
Aswin Siddalingam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2016 07:49 AM
Built in Phone Number fields in ServiceNow handle this by default for you. If this is a custom field, you could look to create a Client Script to handle this for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2016 09:12 AM
Yup I Figured it out.
I Have written below client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue.indexOf('(') != 0)
{
var inputPhone = newValue;
var inputPhone1 = inputPhone.substr(0,3);
var inputPhone2 = inputPhone.substr(3,3);
var inputPhone3 = inputPhone.substr(6,4);
var newPhone = '('+inputPhone1+')'+inputPhone2+'-'+inputPhone3;
g_form.setValue('u_phone',newPhone);
}
}
Thanks ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2016 09:43 AM
Here is one with a little more error checking for you:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var userPhone = '';
var newNum = '';
userPhone = g_form.getValue('u_telephone_number');
userPhone = userPhone.replace(/[^\d]/g,'');
//g_form.showFieldMsg('u_telephone_number',"phone number = " + userPhone,'error');
var testDigitsPattern = /^[0-9]{10}$/;
var testResult = testDigitsPattern.test(userPhone);
//g_form.showFieldMsg('u_telephone_number',"Result = " + testResult,"error");
if(!testDigitsPattern.test(userPhone)){
g_form.clearValue('u_telephone_number');
g_form.showFieldMsg('u_telephone_number', "Invalid telephone number. Please enter your telephone number including area code.",'error');
}
if(testDigitsPattern.test(userPhone)){
g_form.hideAllFieldMsgs();
newNum = '(' + userPhone.substring(0,3) + ') ' + userPhone.substring(3,6) + ' - ' + userPhone.substring(6,10);
g_form.setValue('u_telephone_number',newNum);
}
}