REGEX that requires an underscore and max of 10 characters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 02:41 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 07:17 PM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 08:10 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader