ReferenceError: PasswordValidator is not defined.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2024 11:10 PM - edited ‎10-23-2024 01:38 AM
Test
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 02:17 AM
The provided script looks like a typical ServiceNow client script where a password validation is done via a GlideAjax call to a Script Include. Here’s a refined version of the code with proper formatting:
function onChange(control, oldValue, newValue, isLoading) {
// Check if the form is loading or if the new value is empty
if (isLoading || newValue === '') {
return;
}
// Create a GlideAjax object
var ga = new GlideAjax('PasswordValidator');
// Add parameters: call the 'validatePassword' method in the Script Include
ga.addParam('sysparm_name', 'validatePassword');
// Pass the new password value to the Script Include
ga.addParam('password', newValue);
// Send the request and process the response
ga.getXMLAnswer(function(response) {
// Parse the JSON response
var result = JSON.parse(response);
// Check if the password is valid
if (!result.isValid) {
// Construct the error message
var errorMsg = "Password validation failed:\n" + result.errors.join("\n");
// Show an alert with the error message
alert(errorMsg);
// Clear the password field
g_form.setValue('password', '');
// Optionally, set an error message on the field
control.setError(errorMsg);
}
});
}
Explanation:
1. GlideAjax Call: The GlideAjax object is used to call the PasswordValidator Script Include’s validatePassword method, passing the new password as a parameter.
2. Response Handling: The response is expected to be a JSON object, which is parsed. If the password validation fails (!result.isValid), the errors are displayed, the password field is cleared, and an error is set on the field.
This should be functional if your Script Include (PasswordValidator) is correctly configured to handle the validatePassword method and return a JSON-formatted response.