phone number validation

huyjor
Tera Contributor

Hello everyone

I'm trying to write this client script for the phone number on the application form. I look at the script. It seems to be right but i'm not sure why it's not doing anything. no error. Am I tripping or something else? please help. Thanks 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var phoneNumbReg = /^\d{3}-\d{3}-\d{4}$/;
if (!phoneNumbReg.test(newValue)) {
g_form.showFieldMsg('phone_number', 'Please enter a valid phone number', 'error');
g_form.clearValue('phone_number');
} else {
g_form.hideFieldMsg('phone_number');
}

}

1 ACCEPTED SOLUTION

Sainath N
Mega Sage
Mega Sage

@huyjor : Please try with the below code. 

 

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', '');
    }
}

 

ServiceNow HI Article that tells the implementation steps in detail: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0719284

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

View solution in original post

5 REPLIES 5

Harish Bainsla
Tera Sage
Tera Sage