Regex Validation with ONLY uppercase letters, numbers, space and special characters.

Austine
Tera Contributor

Hello everyone,

 

Please I have a requirement to write a regex validation that accommodates, ONLY uppercase letters, numbers, space, and special characters such as: !@#$%&*_-,

 

How do I this please? Thanks for your continuous support.

3 ACCEPTED SOLUTIONS

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @Austine ,

 

Try this regex pattern- /^[A-Z0-9 !@#$%&*_-]+$/

Sample code :

// String to validate
var input = "";

// Define the regex pattern: only uppercase letters, numbers, spaces, and special characters
var pattern = /^[A-Z0-9 !@#$%&*_-]+$/;

// Test the string against the pattern
var isValid = pattern.test(input);

// Display the result
if (isValid) {
    gs.info("Valid input!");
} else {
    gs.info("Invalid input!");
}

 

If this solution helps you then, mark it as accepted solution ‌‌✔️ and give thumbs up 👍!

 

Thanks,

Alka

 

View solution in original post

Gangadhar Ravi
Giga Sage
Giga Sage

@Austine Please try this.

 

^[A-Z0-9 !@#$%&*_-]+$

 Please mark my answer correct and helpful if this works for you.

View solution in original post

SyamPrasanM
Tera Expert

Hi @Austine ,

Hope this Answer is useful to your requirement . 


To create a regex validation in ServiceNow that accommodates only uppercase letters, numbers, spaces, and specific special characters (!@#$%&*_-,), you can use the following regex pattern:

Explanation of the Regex:

  • ^ : Asserts the start of the string.
  • [A-Z0-9 !@#$%&*_,-] : Defines a character class that allows:
    • A-Z : Uppercase letters.
    • 0-9 : Digits.
    • A space.
    • The special characters !@#$%&*_-,.
  • * : Allows for zero or more occurrences of the characters in the class.
  • $ : Asserts the end of the string.

 

Implementation in ServiceNow:

  1. Go to the field you want to validate.
  2. In the Dictionary entry of the field, find the Validation or Business Rules section.
  3. Enter the regex pattern above in the validation condition.

script if you're using a Business Rule or Script Include:

 

var inputString = current.email; 
var regex = /^[A-Z0-9 !@#$%&*_,-]*$/;

if (!regex.test(inputString)) {
gs.addErrorMessage("Invalid input. Only uppercase letters, numbers, spaces, and special characters (!@#$%&*_,-) are allowed.");
current.setAbortAction(true);
}

Thank you 

Mule Syam Prasanna

Praval tech 

 PRAVAL-LOGO.png

View solution in original post

4 REPLIES 4

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @Austine ,

 

Try this regex pattern- /^[A-Z0-9 !@#$%&*_-]+$/

Sample code :

// String to validate
var input = "";

// Define the regex pattern: only uppercase letters, numbers, spaces, and special characters
var pattern = /^[A-Z0-9 !@#$%&*_-]+$/;

// Test the string against the pattern
var isValid = pattern.test(input);

// Display the result
if (isValid) {
    gs.info("Valid input!");
} else {
    gs.info("Invalid input!");
}

 

If this solution helps you then, mark it as accepted solution ‌‌✔️ and give thumbs up 👍!

 

Thanks,

Alka

 

Gangadhar Ravi
Giga Sage
Giga Sage

@Austine Please try this.

 

^[A-Z0-9 !@#$%&*_-]+$

 Please mark my answer correct and helpful if this works for you.

@Gangadhar Ravi Thank you so much for your support. It works.

SyamPrasanM
Tera Expert

Hi @Austine ,

Hope this Answer is useful to your requirement . 


To create a regex validation in ServiceNow that accommodates only uppercase letters, numbers, spaces, and specific special characters (!@#$%&*_-,), you can use the following regex pattern:

Explanation of the Regex:

  • ^ : Asserts the start of the string.
  • [A-Z0-9 !@#$%&*_,-] : Defines a character class that allows:
    • A-Z : Uppercase letters.
    • 0-9 : Digits.
    • A space.
    • The special characters !@#$%&*_-,.
  • * : Allows for zero or more occurrences of the characters in the class.
  • $ : Asserts the end of the string.

 

Implementation in ServiceNow:

  1. Go to the field you want to validate.
  2. In the Dictionary entry of the field, find the Validation or Business Rules section.
  3. Enter the regex pattern above in the validation condition.

script if you're using a Business Rule or Script Include:

 

var inputString = current.email; 
var regex = /^[A-Z0-9 !@#$%&*_,-]*$/;

if (!regex.test(inputString)) {
gs.addErrorMessage("Invalid input. Only uppercase letters, numbers, spaces, and special characters (!@#$%&*_,-) are allowed.");
current.setAbortAction(true);
}

Thank you 

Mule Syam Prasanna

Praval tech 

 PRAVAL-LOGO.png