我将尽力解释这一点。好的,这是我的代码:
function validation() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var subject = document.getElementById("subject").value;
var message = document.getElementById("message").value;
var at = "@";
var dot = ".";
var lat = email.indexOf(at);
var lstr = email.length;
var ldot = email.indexOf(dot);
var success = true;
if (name == null || name == "") {
success = false;
document.getElementById("name-error").innerHTML = "I want to know who you are!";
}
if (document.contact.name.value.length <= 5) {
success = false;
document.getElementById("name-error").innerHTML = "Full name, please!";
}
if (email.indexOf(at) == -1 || email.indexOf(at) == 0 || email.indexOf(at) == lstr) {
success = false;
document.getElementById("email-error").innerHTML = "That's not an email!";
}
if (email == null || email == "") {
success = false;
document.getElementById("email-error").innerHTML = "Give me your email!";
}
if (document.contact.email.value.length <= 8) {
success = false;
document.getElementById("email-error").innerHTML = "Email is too short!";
}
if (subject == null || subject == "") {
success = false;
document.getElementById("subject-error").innerHTML = "I need a subject!";
}
if (document.contact.subject.value.length <= 5) {
success = false;
document.getElementById("subject-error").innerHTML = "A longer subject would be nice.";
}
if (message == null || message == "") {
success = false;
document.getElementById("message-error").innerHTML = "Don't forget your message!";
}
if (document.contact.message.value.length <= 30) {
success = false;
document.getElementById("message-error").innerHTML = "Your message is too concise!";
}
return success;
}
我的问题是,即使 id 的值为空,它仍然说它太短了。例如,当我单击发送按钮时,它说名称字段太短,即使它是空的。我以为我说过如果 name == "" (为空),请说“我想知道你是谁!”。但是现在,它只是说,“请全名”,如果 name 的值 <= 5 则应该显示...而不是当它为空时。明白我在说什么?