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

Maik Skoddow
Tera Patron
Tera Patron

Hi @huyjor 

Are you sure that the field name is "phone_number"? Custom fields always start with the prefix "u_".

Maik

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.

@huyjor : Did my response address your question, or is there anything else I can help you with?

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

Nivedita Patil
Mega Sage
Mega Sage

Hi @huyjor ,

 

Please refer the below link.

https://www.servicenow.com/community/developer-forum/mobile-number-validation/m-p/1866580

 

Mark my answer as accepted solution and helpful if helps you.

 

Thanks,

Nivedita Patil.