How to change default “please fill out this field” in two field

后端 未结 4 736
无人共我
无人共我 2021-02-13 18:30

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

4条回答
  •  执笔经年
    2021-02-13 18:59

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

提交回复
热议问题