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
Tera Contributor

Hi@AshishKM 

 

I created new regular expression but received the error message - Not a valid regular expression. Please provide just the regular expression.

RiyaJain_0-1747991501510.png

 

Regards,

Riya

@Ria 

Did you try the script I shared yesterday?

in the above image don't give the / at the end and try saving?

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 removed the / at the end and tried to save again, but the issue still persists.

 

Regards,

Riya

Hi,

Skip the initial and ending / sign when creating a regular variable expression.

Those characters are only needed if you were to script the expression as part of a business rule or a client script.

Ankur Bawiskar
Tera Patron
Tera Patron

@Ria 

you can use onChange client script on your field or variable and use this RegEx

something like this, please enhance

// onChange Client Script for mobile_number field
// Type: onChange
// Field name: mobile_number

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

    // RegEx: Optional '+' at start, followed by at least 10 digits, nothing else
    var regex = /^\+?\d{10,}$/;

    // Check for multiple '+' signs
    var plusCount = (newValue.match(/\+/g) || []).length;

    if (!regex.test(newValue) || plusCount > 1) {
        g_form.showFieldMsg('mobile_number', 'Please enter a valid mobile number: minimum 10 digits, only one "+" allowed at the start.', '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