Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

REGEX that requires an underscore and max of 10 characters

kdelbridge
Tera Expert

I have a requirement for a variable to have the following requirements when user is filling out a form via our Employee Center.

 

  • cannot exceed 10 characters
  • must contain an underscore (_)
  • Must contain site identifier

I have already help text example  "DH_1234567", but really wanted to ensure we ensure no more than 10 characters and contains underscore.

 

I have tried the following and it is not working;

 

^[A-Z]{2}_[0-9]{1,7}$

 

and I also tried this

 

^(?i)[A-Za-z0-9]*_[0-9]{1,7}$

 

neither of these worked. Can anyone assist with what I might be missing.

 

Thanks in advance.

 

 

2 REPLIES 2

Swapna Abburi
Giga Sage

Hi @kdelbridge 

If the validation is just to ensure length of variable value and underscore, you can use below script. No regex is needed.

var variableValue = g_form.getValue("variable_name");

if (variableValue.length>10 || variableValue.indexOf('_')<0)

Ankur Bawiskar
Tera Patron

@kdelbridge 

this should work fine

^[A-Z]{2}_[0-9]{1,7}$

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader