0

我将尽力解释这一点。好的,这是我的代码:

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 则应该显示...而不是当它为空时。明白我在说什么?

4

3 回答 3

2

把它变成一个elseif而不是有两个独立的独立条件:

if(name == null || name == "") {
success = false;
  document.getElementById("name-error").innerHTML = "I want to know who you are!";
}
else if(document.contact.name.value.length <= 5) {
 success = false;
document.getElementById("name-error").innerHTML = "Full name, please!";  
}
于 2013-06-12T06:21:36.270 回答
1

您仅提及与字段相关的所有条件,if但实际上您需要将它们提及为if.. elseorif... else if.. else语句。

如果您仅提及 if 语句,那么它将执行所有相关代码到一个字段,最后它会在您的“电子邮件字段”条件中显示您分配的后一个值,innerHTML该值始终是“电子邮件太短! ”。

于 2013-06-12T06:21:19.353 回答
0

这是您的修复代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form validation</title>
<script>
function validation() {

    var name = document.getElementById("name").value.trim();
    var email = document.getElementById("email").value.trim();
    var subject = document.getElementById("subject").value.trim();
    var message = document.getElementById("message").value.trim();
    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.length)==0) {
        success = false;
        document.getElementById("name-error").innerHTML = "I want to know who you are!";
    }else{          
        document.getElementById("name-error").innerHTML = "";
    }

    if(name.length > 1 && document.contact.name.value.length < 5) {
        success = false;
        document.getElementById("name-error").innerHTML = "Full name, please!";  
    }

    if(email == null || email.length==0) {
        success = false;
        document.getElementById("email-error").innerHTML = "Give me your email!";
    }else{
        document.getElementById("email-error").innerHTML = "";
    }       

    if(email.length > 1 && document.contact.email.value.length <= 8){
        success = false;
        document.getElementById("email-error").innerHTML = "Email is too short!";     
    }

    if(email.length > 8 && 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(subject == null || subject.length==0) {
        success = false;
        document.getElementById("subject-error").innerHTML = "I need a subject!";
    }else{
        document.getElementById("subject-error").innerHTML = "";
    }

    if(subject.length > 0 && document.contact.subject.value.length <= 5){
        success = false;
        document.getElementById("subject-error").innerHTML = "A longer subject would be nice.";  
    }

    if(message == null || message.length==0) {
        success = false;
        document.getElementById("message-error").innerHTML = "Don't forget your message!";
    }else{
        document.getElementById("message-error").innerHTML = "";
    }

    if(message.length > 0 && document.contact.message.value.length <= 30){
        success = false;
        document.getElementById("message-error").innerHTML = "Your message is too concise!"; 
    }

    return success;

}
</script>
</head>
<body>
<form action="" method="post" name='contact'>
    <input type="text" name="name" id="name" value=''/>
    <span id="name-error"></span>
    <br />
    <input type="text" name="email" id="email" value=''/>
    <span id="email-error"></span>
    <br />
    <input type="text" name="subject" id="subject" value=''/>
    <span id="subject-error"></span>
    <br />
    <input type="text" name="message" id="message" value=''/>
    <span id="message-error"></span>
    <br />
    <input type="button" value="Send" onclick='return validation();'>
</form>

</body>
</html>

小提琴演示

于 2013-06-12T07:50:49.233 回答