password validator is not defined

kun1
Tera Expert

Hi Team,

 

I am using password validation script for one of the catalog item using script include but getting error : ReferenceError: PasswordValidator is not defined.

 

Script include

kun1_0-1729657550122.png

 

var PasswordValidator = Class.create();
PasswordValidator.prototype = {
    initialize: function() {},

    validate: function(password) {
        var isValid = true;
        var errors = [];

        // Check length
        if (password.length < 8) {
            isValid = false;
            errors.push("Password must be at least 8 characters long.");
        }

        // Check for uppercase
        if (!/[A-Z]/.test(password)) {
            isValid = false;
            errors.push("Password must contain at least one uppercase letter.");
        }

        // Check for lowercase
        if (!/[a-z]/.test(password)) {
            isValid = false;
            errors.push("Password must contain at least one lowercase letter.");
        }

        // Check for number
        if (!/[0-9]/.test(password)) {
            isValid = false;
            errors.push("Password must contain at least one number.");
        }

        // Check for special character
        if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
            isValid = false;
            errors.push("Password must contain at least one special character.");
        }

        return {
            isValid: isValid,
            errors: errors
        };
    },

    type: 'PasswordValidator'
};
 
Catalog client script
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var validator = new PasswordValidator();
    var result = validator.validate(newValue);
    if (!result.isValid) {
        var errorMsg = "Password validation failed:\n" + result.errors.join("\n");
        alert(errorMsg);
        g_form.setValue('password', '')
        control.setError(errorMsg); // Optionally set an error on the field
    }
}
9 REPLIES 9

@kun1 Yes, the back-end variable name being new_password can definitely impact the code, as it should match the parameter you're passing from the client script.

 

1. Script Include:

var PasswordValidator = Class.create();
PasswordValidator.prototype = {
    initialize: function() {},

    // Function called from the Client Script via GlideAjax
    validatePassword: function() {
        // Use request.getParameter with the correct variable name
        var password = request.getParameter('sysparm_new_password');  // Updated to match your back-end variable name
        var result = this.validate(password);  // Call the validation logic
        
        return JSON.stringify(result);  // Return the result as JSON
    },

    validate: function(password) {
        var isValid = true;
        var errors = [];

        // Validation logic (same as your existing code)
        if (password.length < 😎 {
            isValid = false;
            errors.push("Password must be at least 8 characters long.");
        }

        if (!/[A-Z]/.test(password)) {
            isValid = false;
            errors.push("Password must contain at least one uppercase letter.");
        }

        if (!/[a-z]/.test(password)) {
            isValid = false;
            errors.push("Password must contain at least one lowercase letter.");
        }

        if (!/[0-9]/.test(password)) {
            isValid = false;
            errors.push("Password must contain at least one number.");
        }

        if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
            isValid = false;
            errors.push("Password must contain at least one special character.");
        }

        return {
            isValid: isValid,
            errors: errors
        };
    },

    type: 'PasswordValidator'
};

 

2. Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Use GlideAjax to call the Script Include
    var ga = new GlideAjax('PasswordValidator');  // Pass the Script Include name
    ga.addParam('sysparm_name', 'validatePassword');  // Call the 'validatePassword' function in Script Include
    ga.addParam('sysparm_new_password', newValue);  // Updated to match your back-end variable name

    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);  // Parse the result

        if (!result.isValid) {
            var errorMsg = "Password validation failed:\n" + result.errors.join("\n");
            alert(errorMsg);
            g_form.setValue('new_password', '');  // Clear the password field (ensure this matches your field name)
        }
    });
}

Tried with the above code also . Variable type is masked . Please check the screenshot @Aniket Chavan 

 

Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received. 

 

kun1_0-1729669891874.png

 

We can connect over this @Aniket Chavan to check 

GlideFather
Tera Patron

What about this part, the emoji transcription in the script include?


// Validation logic (same as your existing code)
if (password.length < 😎 {

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


this has been changed in the code as per requirement @GlideFather