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
<>tool for create a complete snippet