Skip to content

Commit c577b1b

Browse files
authored
Adding code snippet to validate user profile (#1547)
1 parent ea95bcc commit c577b1b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Overview
2+
This JavaScript code snippet is designed to validate user profile fields in ServiceNow before submission. It is particularly useful for ServiceNow developers looking to implement robust data validation mechanisms in user profiles.
3+
4+
# How It Works
5+
The snippet uses a business rule that executes on form submission to validate user input:
6+
- **Regular Expressions**: It utilizes regex patterns to check the format of the `phone` and `email` fields.
7+
- **Error Messaging**: If the validation fails, an error message is displayed to the user, and the submission is aborted.
8+
9+
# Implementation
10+
- **Add to a Business Rule**: This snippet should be incorporated into a business rule configured to run on the user profile table.
11+
- **Adjust Validation Patterns**: Modify the `phoneNumberPattern` and `emailPattern` variables to align with your specific requirements (e.g: international phone formats).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Script for validating user profile fields on submit
2+
(function executeRule(current, previous /*null when async*/) {
3+
var phoneNumberPattern = /^[0-9]{10}$/; // Modify as needed
4+
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
5+
6+
// Validate phone number
7+
if (!phoneNumberPattern.test(current.phone)) {
8+
gs.addErrorMessage("Phone number must be a 10-digit number.");
9+
current.setAbortAction(true);
10+
}
11+
12+
// Validate email format
13+
if (!emailPattern.test(current.email)) {
14+
gs.addErrorMessage("Invalid email format.");
15+
current.setAbortAction(true);
16+
}
17+
})(current, previous);

0 commit comments

Comments
 (0)