ReferenceError: PasswordValidator is not defined.

kun1
Tera Expert

Test

5 REPLIES 5

Sindhup
Tera Contributor

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.