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 

Did you get a chance to check the solution I shared above?

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

Hi @Ankur Bawiskar 

 

I checked the above script and it's accepting more then 10 digits and no error message is displayed.

RiyaJain_0-1748243833267.png

 

 

 

Regards,

Riya

@Ria 

try this

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

    // Remove all spaces from the input for validation
    var value = newValue.replace(/\s/g, '');

    // RegEx Explanation:
    // ^\+?         : Optional '+' at the start
    // \d{10,}      : At least 10 digits
    // $            : End of string
    var regex = /^\+?\d{10,}$/;

    // Count of '+' signs
    var plusCount = (value.match(/\+/g) || []).length;

    // Validation checks:
    // 1. Only one '+' at the start (if any)
    // 2. At least 10 digits
    // 3. Only digits allowed after optional '+'
    if (
        !regex.test(value) ||                // Fails the main format
        (plusCount > 1) ||                   // More than one '+'
        (plusCount === 1 && value.charAt(0) !== '+') // '+' not at start
    ) {
        g_form.showFieldMsg(
            'mobile_number', 
            'Please enter a valid mobile number: minimum 10 digits, only one "+" allowed at the start, digits only.', 
            '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 

I checked the above script and it's accepting more then 10 digits again and no error message is displayed.

RiyaJain_0-1748244974864.png

 

Regards,

Riya

@Ria 

In your question you said minimum 10 numbers so it will accept more than 10 as well

What's your actual business requirement?

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