0

I use Bootstrap 4 / JS form validation (https://getbootstrap.com/docs/4.0/components/forms/#custom-styles) which works as intended.

I am trying to add the same formatting and submission rules for a comparison of the two password fields, password ('#pw') and repeat password ('#pwRepeat'), so that it shows the green cell border and checkmark icon when they match and the red cell border and error icon when they don't match.

My problem is that I can't get it to trigger the invalid format resp. to set it manually to invalid when the passwords don't match (for corresponding lines see comments below).

Can someone show me how to do this right and also provide a short explanation ?

My JS:

(function() {
    'use strict';
    window.addEventListener('load', function() {
        var forms = document.getElementsByClassName('needs-validation');
        
        var validation = Array.prototype.filter.call(forms, function (form) {
            form.addEventListener('submit', function (event) {
                var pw = document.getElementById('pw').value; // $('#pw').val();
                var pwRepeat = document.getElementById('pwRepeat').value; // $('#pwRepeat').val();

                if(pwRepeat != pw) {
                    // set cell border color to red and show error icon for invalid input
                    pwRepeat.addClass('invalid'); // not working
                }
                if(form.checkValidity() === false) {
                    event.preventDefault();
                    event.stopPropagation();
                }
                form.classList.add('was-validated');    

                // submit if no errors where found
                if((pwRepeat == pw) && (form.checkValidity() === true)) {
                    // do stuff
                }

            }, false);
        });
    },
    false);
})();

Many thanks in advance,
Tim

1
  • Please use <> tool for create a complete snippet Commented Mar 2, 2021 at 10:12

1 Answer 1

1

Couple of issues here.

var pwRepeat = document.getElementById('pwRepeat').value;

pwRepeat is a value and not an element so the following wouldn't work and addClass isn't correct either.

pwRepeat.addClass('invalid'); 

Replace the above with:

 document.getElementById('pwRepeat').classList.add("invalid");

Given there is no working example the above is untested but should point you in the right direction as to why this currently doesn't work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for this. I mixed JS and jQuery here and was too fast in typing this. I will try to create a working example to find out why the colors are still not being set.
Probably .invalid css border colour isn't overriding the existing border colour. Trying adding !important to it. like .invalid { border : solid 1px red !important;} Failing that, post the CSS and the HTML.
Thank you. I realised that the required default classes are called is-invalid / is-valid and not invalid / valid. Changing this seems to work with the borders and icons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.