password validator is not defined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 09:26 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 11:54 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 12:06 AM
getting unhandled exception error
Script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 12:09 AM
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
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 12:16 AM - edited 10-23-2024 12:33 AM
@Aniket Chavan Still the same. back end value of variable is new_password. is this impact here in the code?