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

The same configuration works for me.

Please share your setup.

Below is how I made mine.
regex-10digits-and-plus-sign.png

Ria
Tera Contributor

@OlaN 

 

RiyaJain_1-1748262246000.png

 

I saw that you have left the ending comma in the regex, that means you will accept 10 or more digits in the input.
If you want exactly 10 digits (I saw something about that it in this thread), then remove that comma (like in my picture).

Also, if you are using this variable validation regex, then no additional client script is needed to validate the input.
Check your clients scripts, to see if they are interfering with the regex you've set up.

Ria
Tera Contributor

@OlaN 

RiyaJain_0-1748264319625.png

Still it shows error message.

That error message comes from something else, like a catalog client script, you haven't configured any form of message in your variable validation regex.