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-23-2024 12:46 AM
@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)
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 12:51 AM - edited 10-23-2024 01:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 01:11 AM
We can connect over this @Aniket Chavan to check
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 12:55 AM
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! */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 12:56 AM
this has been changed in the code as per requirement @GlideFather