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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

35 REPLIES 35

Lutuf Ali Shah1
Tera Expert

Hi @Ria 

You can utilize onchange Client script on mobile number field to validate if entered value matches 10 digits including only one "+" sign.

 

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

   // Get the mobile phone value
   var phone = g_form.getValue('u_mobile_number');

   // Define regex: '+' required at start, followed by exactly 10 digits
   var regex = /^\+\d{10}$/;

   // Validate
   if (phone && !regex.test(phone)) {
       g_form.showFieldMsg('u_mobile_number', 'Please enter a valid mobile number (+ followed by exactly 10 digits).', 'error');
   } else {
       g_form.hideFieldMsg('u_mobile_number'); // Hide error if valid
   }
}

 

What will be accepted (valid examples):

Input Reason

+1234567890Exactly + followed by 10 digits
+0987654321Exactly + followed by 10 digits
  

What will NOT be accepted (invalid examples):

Input Reason

1234567890Missing the required + at start
+123456789Only 9 digits (too short)
+1234567890111 digits (too long)
+12345abcdeContains letters, not digits
++1234567890Two plus signs, invalid
+1234567890Leading space before +
+1234567890Trailing space after digits

@Lutuf Ali Shah1 

 

Could you please let me know why changes are not reflecting on portal but changes are reflecting on backend?

@Ria please select UI Type to all so your client script should be able to run on portal as well.

 

OlaN
Giga Sage
Giga Sage

Hi,

This regular expression will allow for an input of an optional + sign at the beginning, then 10 or more numbers.

^[+]?[\d]{10,}$

Will that work for you ?

And if you want to enforce the + sign in the beginning, just remove the question-mark from the expression.

Ria
Tera Contributor

Hi @OlaN 

 

I’ve created a new regular expression, but even after entering the correct details, it still returns a null error. The error message should be cleared once valid input is provided.

RiyaJain_0-1748260456116.png