I would like two different messages in two field, for example, the username and password field that contain messages like \"username cannot be blank\" and \"password cannot be b
sorry, some mistakes in my code. Try that, that works for me :
$(document).ready(function() {
var msg="";
var elements = document.getElementsByTagName("INPUT");
for (var i = 0; i < elements.length; i++) {
elements[i].oninvalid =function(e) {
if (!e.target.validity.valid) {
switch(e.target.id){
case 'password' :
e.target.setCustomValidity("Bad password");break;
case 'username' :
e.target.setCustomValidity("Username cannot be blank");break;
default : e.target.setCustomValidity("");break;
}
}
};
elements[i].oninput = function(e) {
e.target.setCustomValidity(msg);
};
}
})