How to validate a string field for proper phone number format

quanha
Kilo Contributor

I have a need to validate a string field in a record producer for proper phone number formatting (xxx) xxx-xxxx. I understand that a client script for onSubmit should cover it, but I can't seem to devise a proper script that does the validation. Does anyone have a sample validation script that is working for them?

10 REPLIES 10

Hi,



This code is working fine for me.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') {


          return;


    }


var tempValue = newValue;


//Use Regular Expressions to verify number


var phoneRegex1 = /^\d{3}-\d{3}-\d{4}$/;


var phoneRegex2 = /^(800|866|877)/;


var phoneRegex3 = /^(1{3}-1{3}-1{4}|2{3}-2{3}-2{4}|3{3}-3{3}-3{4}|4{3}-4{3}-4{4}|5{3}-5{3}-5{4}|6{3}-6{3}-6{4}|7{3}-7{3}-7{4}|8{3}-8{3}-8{4}|9{3}-9{3}-9{4}|0{3}-0{3}-0{4})$/;


if(tempValue.match(phoneRegex1) && !tempValue.match(phoneRegex2) && !tempValue.match(phoneRegex3))


{


return;


}


else


{


g_form.setValue('u_working_hrs', '');


alert('Phone number must be in format XXX-XXX-XXXX and must not start with 800, 866, or 877.');


}


}


Regards,


Chidu