Regex Validation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 03:22 AM
Hi All,
I have a requirement that related to regex validation.
Use Case: we have a catalog item which consists of couple of fields. One of the field is user id which is set as a string field. If user enters user id as only characters or only numbers it should allow to submit the request.
It should be only one character and rest are numbers. This is the requirement.
Is there any way to achieve this?. I have seen couple of articles related to this but nothing match with my requirement.
If anyone implemented such things please help me to achieve it.
Thanks in advance.
Ajith Pillai.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 03:39 AM
you can use this regex in variable validation regex and then use that in your variable
1) create new record in this table
2) then use that in variable
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 03:41 AM
Hello,
first create new record in Question Regular Expression table [question_regex]:
- Name: User ID
- Validation message: Not a user ID
- Regular Expression: ^[A-Za-z][0-9]*$
This validates a string where the first character is a letter (A-Z or a-z), and the remaining characters are digits (0-9).
Next, open your variable from Catalog Item, add field Validation Regex to a form and add User ID record you created.
Official ServiceNow documentation: Define a regular expression for a variable
Related article: Field validation: Regular Expressions
If my answer helped you, please mark it as correct and helpful, thank you 👍
Martin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 03:43 AM
Hello @Ajith A Pillai
OnSubmit client script:
// Get the value of the user ID field
var userId = g_form.getValue('user_id');
// Define the regex pattern
var regex = /^[a-zA-Z]\d+$/;
// Test the user ID against the regex pattern
if (!regex.test(userId)) {
// If the validation fails, show an error message and prevent form submission
g_form.showFieldMsg('user_id', 'User ID must start with one character followed by numbers.', 'error');
return false;
}
// If the validation passes, allow form submission
return true;
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 06:37 PM
Hello @Ajith A Pillai
Follow the steps to meet the requirement
- Create a new record in the "question_regex" table.
- Regex Expression : ^[A-Za-z]\d{6}$
- Apply this regex to the required variables.
This validation ensures that the user input meets the following criteria:
- The first character must be an alphabet (A-Z or a-z).
- The next six characters must be digits (0-9).
- The total length must be exactly 7 characters.
Result:
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar