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

@Ria 

it's there.

Sees somebody messed up the catalog client script form in your instance.

This is how it looks OOTB, set that field as ALL from list layout in that case

AnkurBawiskar_0-1748268617268.png

 

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

@Ankur Bawiskar 

Everything is functioning as expected; however, the user has requested that the mobile number field retain its value even after an incorrect entry. To accommodate this, I removed the below mentioned line responsible for clearing the field. As a result, when an invalid value is entered, the field retains the incorrect input and displays an error message. However, during form submission, the invalid value is still being accepted, and an RITM is being created.

g_form.clearValue('mobile_number');

@Ria 

Would you mind marking my response as correct if it helped?

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

@Ankur Bawiskar 

I've marked the response as correct, but could you please assist with the issue mentioned above?

@Ria 

you can use onSubmit to stop the form submission

function onSubmit() {
    var value = g_form.getValue('mobile_number').replace(/\s/g, '');
    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'
        );
        return false; // Prevents form submission
    }
    return true; // Allows submission
}

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