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

Aniket Chavan
Tera Sage
Tera Sage

Hello @sreeshsurendran 

Can you test with the below code and let me know how that works for you.

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 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

 

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


Regards,
Aniket

Hi @Aniket Chavan ,

 

I tried this logic, but one problem which I faced was, it was validating even before I enter the new password. Please check this and let me know.

 

Also, the business requirement is the password should be entered between [" "].

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;
}

 

sreeshsurendran
Tera Guru

Hi @Aniket Chavan ,

 

Thanks for the solution. It's working as expected. Thank you!

 

Regards,

Sreesh