0

我有这个功能

function validateEmailp() {
var two = document.getElementById('email').value;
var first = two.split("@")[1];
var badEmails = ["gmail.com", "yahoo.com"]
if (badEmails.indexOf(first) != -1) {
    document.getElementById("email").value = ""; 
    document.getElementById('emailerrorz').innerText = 'We do not accept free e-mails' 
    return false;
    }   
 return true;
}

HTML 是

<input id="email" onblur="validateEmailp()"><div id="emailerrorz"></div>

用户在输入字段中键入免费电子邮件后,他会收到该错误(我们没有..)但是在他将电子邮件更正为非免费电子邮件后,错误应该会清除。我如何用 javascript 做到这一点?

4

2 回答 2

1

做一个else案例:

if (badEmails.indexOf(first) != -1) {
    document.getElementById("email").value = ""; 
    document.getElementById('emailerrorz').innerText = 'We do not accept free e-mails' 
    return false;
} else {
    document.getElementById('emailerrorz').innerText = ""; // clear error
}
于 2013-10-04T09:55:36.957 回答
1
function validateEmailp() {
    var two = document.getElementById('email').value;
    var first = two.split("@")[1];
    var badEmails = ["gmail.com", "yahoo.com"]
    if (badEmails.indexOf(first) > -1) {
        document.getElementById("email").value = ""; 
        document.getElementById('emailerrorz').innerText = 'We do not accept free e-mails' 
        return false;
    } 
    document.getElementById('emailerrorz').innerText = '' 
    return true;
}
于 2013-10-04T10:00:01.853 回答