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

When you get the "null" error message, that is from the variable validation error, since you haven't specified an error message (according to your last image).

I still believe you have multiple things working and validating the same field at the same time.

Check your scripts.