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

Aniket Chavan
Tera Sage
Tera Sage

Hello @kun1 ,

Please try to use the below updated scripts once and let me know how it works for you.

 

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('password', newValue);  // Pass the password value to validate
    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('password', '');  // Clear the password field
            control.setError(errorMsg);  // Optionally set an error on the field
        }
    });
}

 
|Script Include:

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

    // Function called from the Client Script via GlideAjax
    validatePassword: function() {
        var password = this.getParameter('password');
        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'
};

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

 

kun1_0-1729667142205.png

 

 

getting unhandled exception error 

 

Script include

 

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

    // Function called from the Client Script via GlideAjax
    validatePassword: function() {
        var password = this.getParameter('password');
        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 < 8) {
            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'
};
 
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('password', newValue);  // Pass the password value to validate
    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('password', '');  // Clear the password field
           
        }
    });
}
 Please check if any updation required

Hello @kun1 ,

Please try with the updated script below and see how it works:


Updated Script Include:

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

    // Function called from the Client Script via GlideAjax
    validatePassword: function() {
        // Use request.getParameter instead of this.getParameter
        var password = request.getParameter('sysparm_password');  // Corrected parameter 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'
};

 

Updated 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_password', newValue);  // Pass the password value (note the 'sysparm_password')

    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('password', '');  // Clear the password field
        }
    });
}

 

@Aniket Chavan   Still the same. back end value of variable is new_password. is this impact here in the code?