Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Validate password in Catalog Item

sreeshsurendran
Tera Guru

In a catalog item, it has two fields enter password and re-enter password, a logic should be built to validate if both passwords are same.

 

My Approach:

 

Used a Masked field type for both and tried running an onChange client script but seems something's wrong.

 

Code for client script:

 

var password = g_form.getValue('new_psk');
    var pass = g_form.decrypt('new_psk');
    // Your validation logic here
    var regex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+]+$/;
 
    if (!regex.test) {
        alert('Password must be within double quotes and contain at least 1 uppercase, 1 lowercase, 1 number, and 1 special character.');
        return false; // Prevent form submission
    }
1 ACCEPTED SOLUTION

Hello @sreeshsurendran ,

 

In that condition can you please change the logic from onChange to onSubmit and also please test using below code and let me know your views on this.

 

function onSubmit() {
    var password = g_form.getValue('new_psk');
    var confirmPassword = g_form.getValue('confirm_psk');

    // Your validation logic here
    var regex = /^\["(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+]+"\]$/;

    if (!regex.test(password)) {
        alert('Password must be enclosed within square brackets [" "] and contain at least 1 uppercase, 1 lowercase, 1 number, and 1 special character.');
        g_form.setValue('new_psk', ''); // Clear the password field
        g_form.setValue('confirm_psk', ''); // Clear the confirm password field
        return false; // Prevent form submission
    }

    // Check if the passwords match
    if (password !== confirmPassword) {
        alert('Passwords do not match.');
        g_form.setValue('new_psk', ''); // Clear the password field
        g_form.setValue('confirm_psk', ''); // Clear the confirm password field
        return false; // Prevent form submission
    }

    // Continue with form submission or any other logic as needed
    return true;
}

 

View solution in original post

5 REPLIES 5

Hello @sreeshsurendran ,

Welcome and glad to hear that the solution worked for you and please mark my solution 👍Helpful.😁

Thanks,
Aniket.