如何更改默认值“请填写此字段”在两个领域 [英] How to change default "please fill out this field" in two field

查看:209
本文介绍了如何更改默认值“请填写此字段”在两个领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个字段中输入两个不同的消息,例如,用户名和密码字段包含用户名不能为空和密码不能为空等消息。我只是设法改变了消息,但两个字段都是一样的。这是这里

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 blank". I only managed to change the message, but it is then the same for both fields. It's here

$(document).ready(function() {
var elements = document.getElementsByTagName("INPUT");
for (var i = 0; i < elements.length; i++) {
    elements[i].oninvalid = function(e) {
        e.target.setCustomValidity("");
        if (!e.target.validity.valid) {
            e.target.setCustomValidity("Username cannot be blank");
        }
    };
    elements[i].oninput = function(e) {
        e.target.setCustomValidity("");
    };
} })


推荐答案

抱歉,我的代码有些错误。试试这个,对我有用:

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

这篇关于如何更改默认值“请填写此字段”在两个领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆