- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 09:22 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 11:47 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 12:28 PM
@Austine Please try this.
^[A-Z0-9 !@#$%&*_-]+$
Please mark my answer correct and helpful if this works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 10:48 PM
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:
- Go to the field you want to validate.
- In the Dictionary entry of the field, find the Validation or Business Rules section.
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 11:47 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 12:28 PM
@Austine Please try this.
^[A-Z0-9 !@#$%&*_-]+$
Please mark my answer correct and helpful if this works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 01:43 PM
@Gangadhar Ravi Thank you so much for your support. It works.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 10:48 PM
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:
- Go to the field you want to validate.
- In the Dictionary entry of the field, find the Validation or Business Rules section.
- 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