2015-11-02 46 views
0

這個程序運行正常..我的任務是,當名稱字段留空(那麼請輸入您的名字)這個錯誤將會生成,當我輸入錯誤的輸入(如數字)然後(只允許字母和空格)這個錯誤信息必須被生成。我怎樣才能做到這一點。JavaScript表單驗證爲空字段和錯誤的輸入

<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<title>Validation tutorial</title> 
<script> 
function validateName(x){ 
    // Validation rule 

    var re = /[A-Za-z -']$/; 
    // Check input 
    if(re.test(document.getElementById(x).value)){ 
    // Style green 
    document.getElementById(x).style.borderColor ='#3BFF3B'; 
    // Hide error prompt 
    document.getElementById(x + 'Error').style.display = "none"; 
    return true; 
    }else{ 
    // Style red 
    document.getElementById(x).style.borderColor ='#FF0000'; 
    // Show error prompt 
    document.getElementById(x + 'Error').style.display = "block"; 
    return false; 
    } 
} 
// Validate email 
function validateEmail(email){ 
    var re = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
    if(re.test(email)){ 
    document.getElementById('email').style.borderColor ='#3BFF3B'; 
    document.getElementById('emailError').style.display = "none"; 
    return true; 
    }else{ 
    document.getElementById('email').style.borderColor ='#FF0000'; 
    return false; 
    } 
} 
// Validate Select boxes 
function validateSelect(x){ 
    if(document.getElementById(x).selectedIndex !== 0){ 
    document.getElementById(x).style.borderColor ='#3BFF3B'; 
    document.getElementById(x + 'Error').style.display = "none"; 
    return true; 
    }else{ 
    document.getElementById(x).style.borderColor ='#FF0000'; 
    return false; 
    } 
} 
function validateRadio(x){ 
    if(document.getElementById(x).checked){ 
    return true; 
    }else{ 
    return false; 
    } 
} 
function validateCheckbox(x){ 
    if(document.getElementById(x).checked){ 
    return true; 
    } 
    return false; 
} 
function validateForm(){ 
    // Set error catcher 
    var error = 0; 
    // Check name 
    if(!validateName('name')){ 
    document.getElementById('nameError').style.display = "block"; 
    error++; 
    } 
    // Validate email 
    if(!validateEmail(document.getElementById('email').value)){ 
    document.getElementById('emailError').style.display = "block"; 
    error++; 
    } 
    // Validate animal dropdown box 
    if(!validateSelect('subject')){ 
    document.getElementById('subjectError').style.display = "block"; 
    error++; 
    } 
    // Validate Radio 
    if(validateRadio('male')){ 

    }else if(validateRadio('female')){ 

    }else{ 
    document.getElementById('genderError').style.display = "block"; 
    error++; 
    } 

    // Don't submit form if there are errors 
    if(error > 0){ 
    return false; 
    } 
    }  
    </script> 
    </head> 
    <body> 
    <form action="" onsubmit="return validateForm()"> 

    <label for="name">Name</label> 
    <input type="text" name="name" id="name" onblur="validateName(name)" /> 
    <span id="nameError" style="display: none;">only alphabates and white space are allowed</span> 
    <br><br> 
    <label for="email">Email</label> 
    <input type="text" name="email" id="email" onblur="validateEmail(value)" /> 
    <span id="emailError" style="display: none;">You must enter a valid email address</span> 
<br><br> 

    <label for="hand">Gender</label> 
    <ul> 
    <li> 
     <input type="radio" name="gender" id="male" value="male" onblur="validateRadio(id)" /> 
     <label for="left">male</label> 
    </li> 
    <li> 
     <input type="radio" name="gender" id="female" value="female" onblur="validateRadio(id)" /> 
     <label for="right">female</label> 
    </li> 
    </ul> 
    <span class="validateError" id="genderError" style="display: none;">Please select gender</span> 
<br><br> 
<label for="subject">Favourite Subject</label> 
    <select name="subject" id="subject" onblur="validateSelect(name)"> 
    <option value="">SUBJECTS</option> 
    <option value="php">PHP</option> 
    <option value="java">JAVA</option> 
    <option value="sql">SQL</option> 
    </select> 
    <span class="validateError" id="subjectError" style="display: none;">You must select your favourite subject</span> 
    <br><br> 

    <input type="submit" id="submit" name="submit" value="Submit" /> 

</form> 
</body> 
</html> 
+0

在問一個已經回答了100萬次的問題之前做一些研究。 [檢查這裏](http://www.w3schools.com/js/js_validation.asp)有一個簡單的例子,我所做的只是搜索。 – Theunis

+0

沒有這是與php –

+0

@Theunis我研究了所有這一切,但我是新的JavaScript的所以這就是爲什麼問這個問題..如果你知道答案,然後回覆我..其迫切.. –

回答

1
<!doctype html> 
    <html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
<title>Validation tutorial</title> 
<script> 
function validateName(x){ 
    // Validation rule 

    var re = /[A-Za-z -']$/; 
    // Check input 
    if(re.test(document.getElementById(x).value)){ 
    // Style green 
    document.getElementById(x).style.borderColor ='#3BFF3B'; 
    // Hide error prompt 
    document.getElementById(x + 'Error').style.display = "none"; 
    return true; 
    }else if(document.getElementById(x).value === ''){ 
    //This is for an empty string or if name was not entered. 
    // Style red 
    document.getElementById(x).style.borderColor ='#FF0000'; 
    // Show error prompt 
    document.getElementById(x + 'Error2').style.display = "block"; 
    return false; 
    }else{ 
    // Style red 
    document.getElementById(x).style.borderColor ='#FF0000'; 
    // Show error prompt 
    document.getElementById(x + 'Error').style.display = "block"; 
    return false; 
    } 
} 
// Validate email 
function validateEmail(email){ 
    var re = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
    if(re.test(email)){ 
    document.getElementById('email').style.borderColor ='#3BFF3B'; 
    document.getElementById('emailError').style.display = "none"; 
    return true; 
    }else{ 
    document.getElementById('email').style.borderColor ='#FF0000'; 
    return false; 
    } 
} 
// Validate Select boxes 
function validateSelect(x){ 
    if(document.getElementById(x).selectedIndex !== 0){ 
    document.getElementById(x).style.borderColor ='#3BFF3B'; 
    document.getElementById(x + 'Error').style.display = "none"; 
    return true; 
    }else{ 
    document.getElementById(x).style.borderColor ='#FF0000'; 
    return false; 
    } 
} 
function validateRadio(x){ 
    if(document.getElementById(x).checked){ 
    return true; 
    }else{ 
    return false; 
    } 
} 
function validateCheckbox(x){ 
    if(document.getElementById(x).checked){ 
    return true; 
    } 
    return false; 
} 
function validateForm(){ 
    // Set error catcher 
    var error = 0; 
    // Check name 
    if(!validateName('name')){ 
    document.getElementById('nameError').style.display = "block"; 
    error++; 
    } 
    // Validate email 
    if(!validateEmail(document.getElementById('email').value)){ 
    document.getElementById('emailError').style.display = "block"; 
    error++; 
    } 
    // Validate animal dropdown box 
    if(!validateSelect('subject')){ 
    document.getElementById('subjectError').style.display = "block"; 
    error++; 
    } 
    // Validate Radio 
    if(validateRadio('male')){ 

    }else if(validateRadio('female')){ 

    }else{ 
    document.getElementById('genderError').style.display = "block"; 
    error++; 
    } 

    // Don't submit form if there are errors 
    if(error > 0){ 
    return false; 
    } 
    }  
    </script> 
    </head> 
    <body> 
    <form action="" onsubmit="return validateForm()"> 

    <label for="name">Name</label> 
    <input type="text" name="name" id="name" onblur="validateName(name)" /> 
    <span id="nameError" style="display: none;">only alphabates and white space are allowed</span> 
<span id="nameError2" style="display: none;">please enter a name</span> 
    <br><br> 
    <label for="email">Email</label> 
    <input type="text" name="email" id="email" onblur="validateEmail(value)" /> 
    <span id="emailError" style="display: none;">You must enter a valid email address</span> 
<br><br> 

    <label for="hand">Gender</label> 
    <ul> 
    <li> 
     <input type="radio" name="gender" id="male" value="male" onblur="validateRadio(id)" /> 
     <label for="left">male</label> 
    </li> 
    <li> 
     <input type="radio" name="gender" id="female" value="female" onblur="validateRadio(id)" /> 
     <label for="right">female</label> 
    </li> 
    </ul> 
    <span class="validateError" id="genderError" style="display: none;">Please select gender</span> 
<br><br> 
<label for="subject">Favourite Subject</label> 
    <select name="subject" id="subject" onblur="validateSelect(name)"> 
    <option value="">SUBJECTS</option> 
    <option value="php">PHP</option> 
    <option value="java">JAVA</option> 
    <option value="sql">SQL</option> 
    </select> 
    <span class="validateError" id="subjectError" style="display: none;">You must select your favourite subject</span> 
    <br><br> 

    <input type="submit" id="submit" name="submit" value="Submit" /> 

</form> 
</body> 
</html> 

有很多更好的方法來做到這一點。這不是一個優雅的方式。我看到這段代碼來自一個例子或教程。請查看關於這個主題的其他教程,因爲這只是實現你想要的快捷方式。

+0

是的,這是來自一個教程..正如你所說我會檢查其他教程..有沒有任何網站或教程你知道嗎? –

+0

檢查[this](http://www.javascript-coder.com/html-form/javascript-form-validation.phtml)一出 – Theunis

+0

謝謝@Theunis –