Regex Validation

Ajith A Pillai
Tera Contributor

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.

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Ajith A Pillai 

you can use this regex in variable validation regex and then use that in your variable

1) create new record in this table

AnkurBawiskar_0-1739792297114.png

 

2) then use that in variable

AnkurBawiskar_1-1739792361547.png

 

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

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

Martin Friedel
Mega Sage

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

Viraj Hudlikar
Giga Sage

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.

Juhi Poddar
Kilo Patron

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}$

JuhiPoddar_2-1739845659798.png

  • Apply this regex to the required variables.JuhiPoddar_1-1739845483542.png

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:

JuhiPoddar_3-1739845846368.png

JuhiPoddar_4-1739845883873.png

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