regex in client script

Prem13
Tera Contributor

i tried with the below regex

var regexp = /\d{6}@\d{5}/;
var idi = id.match(regexp);

in this before "@" i should be able to enter 6 characters and after "@" it should contain alpha numeric characters  which should be 5 characters in length

1 ACCEPTED SOLUTION

Hi,

I'm not sure what you mean by "where I can enter more than 6 characters before '@' and 5 characters after '@'".

So, yes, you can, but your validation in your client script would then check that against the pattern you've defined and if it doesn't match/work with it...you would clear the field and maybe give an alert to say "please enter in 'x' format".

The regex doesn't literally block someone from entering in a bunch of characters...it's the validation in the client script that would check it.

For example, go to: https://regex101.com/

And in the regular expression field at the top of the website, put: \d{6}@[a-zA-Z0-9]{5}

Then do some test strings for yourself. You'll see on the right, in the Match Information section...they aren't matches UNTIL you do 6 numbers, then a '@' and then 5 characters of letters or numbers.

So for this to then work in SN, you have to add / to front and / to back of the pattern. So it now becomes: 

/\d{6}@[a-zA-Z0-9]{5}/

And then that should work.

But again, users can enter in all sorts of stuff in that field...it's the onChange client script that would check what they enter, against a pattern, and if it doesn't match....you'd clear the value and alert them to the pattern you're expecting.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

9 REPLIES 9

Indrajit
Mega Guru

Hey Prem ,

i think you are trying to validate the email here with regex but you can use below script,

if(oldValue != newValue){

 

   g_form.hideFieldMsg('email', true);

 

   if (!isEmailValid(newValue)) {

         g_form.showFieldMsg('email', newValue + " is an invalid email, please re-enter email in correct format.",'error');

 

 }  }

Or  

You can refer below threads for more,

https://community.servicenow.com/community?id=community_question&sys_id=83b88a49dbb7eb80feb1a851ca96...

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

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

 

kindly mark Correct and Helpful if applicable.

Regards,

Indrajit.

Prem13
Tera Contributor

Hi idrajit,

Im not trying to validate the email id its a single line text field where i need to validate the below format

before "@" i should be able to enter 6 characters (numbers) and after "@" it should contain alpha numeric characters  which should be 5 characters in length

Allen Andreas
Administrator
Administrator

Hi can you try:

var regexp = /\d{6}@[a-zA-Z0-9]{5}/;

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

hi allen,

I need to type only 6 characters  before @ and 5 characters after @ 

is it possible to restrict character limit

Before "@" 6   

After "@" 5 

Since in both before and after i can enter n number of numbers