- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2022 08:22 PM
HI Experts,
Kindly help me, I have a mobile number variable in Single line type.
If entered as 0420111111, it should format as +61 420 111 111 and should happen onChange action.
i tried creating an Onchange catalog client script with below code i found but it is only accepting 10 digits and would not display the correct format. ANY HELP WOULD BE APPRECIATED. THANK YOU!
var pattern = /^\(\d{3}\)\s\d{3}-\d{4}$/; //(xxx) xxx-xxxx
var phone = g_form.getValue('u_mobile_phone');
if (!pattern.test(phone)) {
phone = phone.replace(/\D/g, '');
var regex = /^\d{10}$/;
var is_valid = regex.test(phone);
if (!is_valid) {
g_form.clearValue('u_mobile_phone');
alert('Invalid phone number. Please enter 10 digits');
} else {
phone = '(' + phone.slice(0, 3) + ') ' + phone.slice(3, 6) + '-' + phone.slice(6, 10);
g_form.setValue('u_mobile_phone', phone);
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 06:05 AM
Hi, Try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var pattern = /^\+\d{2}(?:\s\d{3}){3}$/;
if(!pattern.test(newValue))
{
var length = g_form.getValue('u_mobile_phone').length;
if(length != 10)
{
g_form.clearValue('u_mobile_phone');
g_form.showErrorBox('u_mobile_phone', 'enter 10 digits only');
}
else
{
g_form.setValue('u_mobile_phone', '+61 ' + newValue.slice(1,4) + ' ' + newValue.slice(4,7) + ' ' + newValue.slice(7));
}
}
}
Example input : 0420111111
Example Output : +61 420 111 111
Example input : 0420111
Example Output : Enter 10 digits error
Mark as correct and helpful if it solved your query.
Regards,
Sumanth
Regards,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2022 08:24 PM
Hi your checking regex to enter only 10 digits. In string even spaces will be counted so you need to allow atleast 15 digits including spaces
Harish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 05:35 AM
Hello,
Try to do this using below method
https://community.servicenow.com/community?id=community_article&sys_id=ebcf2b6edbd7d0103daa1ea668961973
Regards
Regards,
Musab

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 05:49 AM
Hi,
Use this script for your requirement
var pattern = /^\(\d{3}\)\s\d{3}-\d{4}$/; //(xxx) xxx-xxxx
var phone = g_form.getValue('u_mobile_phone');
if (!pattern.test(phone)) {
phone = phone.replace(/\D/g, '');
var regex = /^\d{10}$/;
var is_valid = regex.test(phone);
if (!is_valid) {
g_form.clearValue('u_mobile_phone');
alert('Invalid phone number. Please enter 10 digits');
} else {
phone = '+61 ' + phone.slice(1, 4) + ' ' + phone.slice(4, 7) + ' ' + phone.slice(7, 10);
g_form.setValue('u_mobile_phone', phone);
}
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 06:04 AM
Hi palanikumar,
I tried this code but it is giving me the alert "Invalid phone number. Please enter 10 digits". Although i have tried several times inputting 10 digits.