The mobile number field must allow a minimum of 10 digits including only one "+" sign.

Ria
Tera Contributor
 
1 ACCEPTED SOLUTION

@Ria 

then use this

// onChange Client Script for mobile_number field
// Type: onChange
// Field name: mobile_number

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }

    // Remove all spaces for validation
    var value = newValue.replace(/\s/g, '');

    // Regex: Must start with '+', followed by exactly 10 digits, nothing else
    var regex = /^\+\d{10}$/;

    if (!regex.test(value)) {
        g_form.showFieldMsg(
            'mobile_number',
            'Please enter a valid mobile number: must start with "+" and be followed by exactly 10 digits.',
            'error'
        );
        g_form.clearValue('mobile_number');
    } else {
        g_form.hideFieldMsg('mobile_number');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

View solution in original post

35 REPLIES 35

AmirMohamed01
Tera Contributor

Could you please elaborate and provide more information?

harish41
Tera Guru

Hi @Ria 

You can try below script. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }


			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', '+' + newValue);
				}
		}
   


You can refer to the scripts mentioned in these threads :
https://www.servicenow.com/community/itsm-forum/help-in-phone-number-formatting-in-variable/m-p/5704...
https://www.servicenow.com/community/developer-articles/field-validation-regular-expressions/ta-p/23...

 

Mark as correct and helpful if it solved your query.

Regards,
Harish

Ria
Tera Contributor

Hi @harish41 

 

I checked the script above, and it currently doesn't accept the plus sign. I would like to include a plus sign followed by 10 digits.

 

Regards,

Riya 

AshishKM
Kilo Patron

Hi @Ria, 

Please share more details, where you are configuring this variable setting.

However, you can configure the Regular Expression and use this regex with phone number variable.

 

/^[+-]?\d{10}$/

 

AshishKM_0-1747930129434.png

 

Let us know if this works for you.

-Thanks,

AshishKM