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

@Ankur Bawiskar 

 

Field should accept input with exactly 10 digits and an one '+' prefix and show an error if the digit count exceeds 10.

@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

@Ankur Bawiskar 

 

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

 

Regards,

Riya

@Ria 

Did you set UI type - ALL for your client script?

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 

On catalog client script we don't have UI type field.

RiyaJain_0-1748268155794.png