Phone number validation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2024 11:10 AM
In catalog form. There is a field call phone number.
User has to enter phone number in that field. And the phone number format is 123-456-7890.
If user doesn't enter in any above format. It shows error.
If user enters in above format . It needs to populate as (123)456-7890.
Require client script for this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2024 11:27 AM
Hi @User_267 ,
You can refer the below accepted solution,
https://www.servicenow.com/community/developer-forum/regex-for-phone-numbers/m-p/1517117
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2024 12:37 PM
Hi @User_267 ,
- Create the following onChange client script:
- Type: onChange
- Variable name: the name of the variable to be used
- Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var pattern = /^[(]?(\d{3})[)]?[-|\s]?(\d{3})[-|\s]?(\d{4})$/;
if (!pattern.test(newValue)) {
alert('Phone enter a valid phone number');
g_form.setValue('variable_name', '');
}
}
*Please note that the above regex / pattern is specific for validating 10 integer digits only. You may modify this regex if there's a need for it to validate any other formats.
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Thanks & Regards,
Sumanth Meda
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2024 06:19 PM
I have the above script, but if someone enters in correct format like 123-456-7890 then it should automatically populate as (123)456-7890 in this format in the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2024 09:28 PM
Hi @User_267
try to add this missing line in your script that will re format the number after entering
var formattedNumber = ‘(’ + match[1] + ‘)’ + match[2] + ‘-’ + match[3];
// Update the field value with the formatted number.
g_form.setValue(control.getName(), formattedNumber);
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards
Deepak Sharma