Skip to content

Adding code snippet to validate user profile #1547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Business Rules/User Profile Field Validation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Overview
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.

# How It Works
The snippet uses a business rule that executes on form submission to validate user input:
- **Regular Expressions**: It utilizes regex patterns to check the format of the `phone` and `email` fields.
- **Error Messaging**: If the validation fails, an error message is displayed to the user, and the submission is aborted.

# Implementation
- **Add to a Business Rule**: This snippet should be incorporated into a business rule configured to run on the user profile table.
- **Adjust Validation Patterns**: Modify the `phoneNumberPattern` and `emailPattern` variables to align with your specific requirements (e.g: international phone formats).
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Script for validating user profile fields on submit
(function executeRule(current, previous /*null when async*/) {
var phoneNumberPattern = /^[0-9]{10}$/; // Modify as needed
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

// Validate phone number
if (!phoneNumberPattern.test(current.phone)) {
gs.addErrorMessage("Phone number must be a 10-digit number.");
current.setAbortAction(true);
}

// Validate email format
if (!emailPattern.test(current.email)) {
gs.addErrorMessage("Invalid email format.");
current.setAbortAction(true);
}
})(current, previous);
Loading