Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

phone number validations which is 8 digits character's limit ,it will allow only numbers (will not allow other charters like Special symbols @,\$,A..)

padhu1
Kilo Expert

Hi Friends,

                                  phone number validations which is 8 digits character's limit ,it will allow only numbers (will not allow other charters like Special symbols @,$,A..) .can any one give me sample code.

Thanks in Advance.

14 REPLIES 14

Sampath8
Tera Expert

Hi Padhu,



I tried the below code and it worked for me.



Script: OnChange.



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


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


return;


}


var pattern = /^[0-9]*$/;


if(!pattern.test(newValue) || (newValue.length <= 7) )// adjust your digits.


{


alert('please enter only digits and min length should be 8 ');


g_form.setValue('upc', ''); // adjust field name accordingly if you want to blank it out.


}


}




Regards,


Sampath.


Dave Smith1
ServiceNow Employee

Does it also work with ABCDEFGH...?   By the looks of your tests, that would also be permitted...


The script which I mentioned will not allow any alphabets to be entered.


Dave Smith1
ServiceNow Employee

I'm surprised, since * means zero2many... but I just tried it in https://regex101.com/ and can confirm it works as intended!


Assuming this comment is intended for me the * is indeed an indicator of zero to many occurrences. But in the following line of code: if(!regex.test(newValue) || newValue.length != 8), I'm checking the length of the newValue. That's why it works.