如何在两个字段中更改默认“请填写此字段”

时间:2013-06-02 19:25:40

标签: html5

我想在两个字段中输入两个不同的消息,例如,用户名和密码字段包含“用户名不能为空”和“密码不能为空”等消息。我只是设法改变了消息,但两个字段都是一样的。这是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("");
    };
} })

4 个答案:

答案 0 :(得分:14)

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

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

答案 1 :(得分:4)

您可以使用此代码。

   <input type="text"  required="" name="username" placeholder="Username" oninvalid="this.setCustomValidity('Username cannot be blank')">
   <input type="password"  required="" name="password" placeholder="Password" oninvalid="this.setCustomValidity('Password cannot be blank')">

答案 2 :(得分:3)

您可以为用户名和密码id元素添加input属性:

<input placeholder="Username" required id="username" />
<input type="password" placeholder="Password" required id="password" />

然后您可以使用switch语句根据事件的目标添加自定义验证消息:

$(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) {
                switch (e.srcElement.id) {
                    case "username":
                        e.target.setCustomValidity("Username cannot be blank");
                        break;
                    case "password":
                        e.target.setCustomValidity("Password cannot be blank");
                        break;
                }
            }
        };
        elements[i].oninput = function (e) {
            e.target.setCustomValidity("");
        };
    }
})

Here是一个演示。

答案 3 :(得分:0)

你在所有输入元素上循环,这就是你在每个元素中获得相同结果的原因。为您的输入提供一个id,例如用户名和密码,然后尝试:

$(document).ready(function() {
var msg="";
var elements = document.getElementsByTagName("INPUT");
for (var i = 0; i < elements.length; i++) {
    elements[i].oninvalid = function(e) {
        e.target.setCustomValidity("");

        switch(e.target.id){
            case "password" :
            msg="Bad password";
            case "username" :
            msg="Username cannot be blank";
        }

        if (!e.target.validity.valid) {
            e.target.setCustomValidity(msg);
        }
    };
    elements[i].oninput = function(e) {
        e.target.setCustomValidity("");
    };
} })