Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

Aman Kumar S
Kilo Patron

Hey,

Please try below script:

var phone= g_form.getValue('u_mobile_phone');
var check = /^[0-9]{10}/;
var verify = phone.match(check);
if(verify == "true"){
         var res = phone.slice(1);
         
         g_form.setValue('u_mobile_phone', '+61 ' + res.slice(0,3) + ' ' + res.slice(3,6) + ' ' + res.slice(6) );
}
else{
          g_form.clearValue('u_mobile_phone');
          alert('Invalid phone number. Please enter 10 digits');
}
Best Regards
Aman Kumar

Hi Aman,

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. šŸ˜ž

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

Thank you, Sumanth! this is working for me! šŸ™‚

@Ljone  Looks like you marked your own answer as correct instead of mine šŸ™‚