MOBILE NUMBER VALIDATION

VISHAL69
Tera Contributor
  • Create any sample table and create sample field mobile no and when user try to submit record following condition should met :

  1. Mobile no should be 11 digit no and must start with 0 always, and should be a valid Indian number. Then submission should work

  2. If conditions fails record should not be submitted.

I want to run client script for this , can anyone help me out?   I want the code which i should run to fulfill my req.

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

Use Variable Validation Regex like so:

find_real_file.png

 

The regular expression:

^[0]\d{10}

 

You can add the validation to a variable like this:

find_real_file.png

 

Or if not in the catalog, you can use an onChange CLient script on the 'phone_number' field:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var reg = /^[0]\d{10}/;
    if (!reg.test(newValue)) {
        g_form.setValue('phone_number', '');
        g_form.showFieldMsg('phone_number', 'Phone number should start with 0 and be 11 digits', "error");
    }
}

View solution in original post

18 REPLIES 18

Onsubmit is a good idea. However, might still be good to have the field message as well. And instead of info message, use error message. Like so:

function onSubmit() {
    var phoneNr = g_form.getValue('phone_number')
    var reg = /^[0]\d{10}/;
    if (!reg.test(phoneNr)) {
        g_form.setValue('phone_number', '');
        g_form.addErrorMessage("Phone number is invallid");
        g_form.showFieldMsg('phone_number', 'Phone number should start with 0 and be 11 digits', "error");
        return false
    }
}

Indrajit
Mega Guru

Hey Vishal,

refer below code ,write onChange client script

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

//Type appropriate comment here, and begin script below
var pattern = /^[(]?(\d{3})[)]?[-|\s]?(\d{3})[-|\s]?(\d{4})$/;

if(!pattern.test(newValue)){
alert('Phone enter a valid phone number');
g_form.setValue('variable_name', '');
}
}

 also you can refer below links for more,

https://community.servicenow.com/community?id=community_question&sys_id=277b8361db9cdbc01dcaf3231f96...

https://community.servicenow.com/community?id=community_question&sys_id=72a80361db5cdbc01dcaf3231f96...

kindly mark Correct and Helpful if applicable.

Regards,

Indrajit.

how do i restrict that phone number must not be enter more than 10 numbers

viswak
Tera Contributor

onSubmit script error: TypeError: pattern.exec is not a function:  function () { [native code]) how to resolve this issue in regex validate phone number Can anyone tell this error