Help in Phone Number formatting in variable

Ljone
Giga Contributor

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);
        }
    }

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

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

View solution in original post

10 REPLIES 10

Harish KM
Kilo Patron
Kilo Patron

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

Regards
Harish

Musab Rasheed
Tera Sage
Tera Sage

Hello,

Try to do this using below method

https://community.servicenow.com/community?id=community_article&sys_id=ebcf2b6edbd7d0103daa1ea668961973

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

palanikumar
Mega Sage

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);
        }
    }
Thank you,
Palani

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.