2014-10-28 48 views
0

我不明白我在這裏做錯了什麼....幾乎所有的嘗試都是在未經驗證的情況下提交表單。我不知道是什麼原因導致這個問題在這裏工作了4個小時,每次我點擊提交按鈕,它會直接提交成功的頁面...任何人都可以幫助我嗎?表單驗證不起作用,對於未經驗證提交的gettting

<script type ="text/javascript"> 
function validate(){ 
if(document.orderForm.firstName.value==""){ 
document.getElementById('errors').innerHTML = "Please Enter a First Name"; 
document.orderForm.fistName.focus(); 
return (false); 
} 
if(document.orderForm.lastName.value == ""){ 
document.getElementById('errors').innerHTML = "Please Enter a Last Name"; 
document.orderForm.lastName.focus(); 
return (false); 
} 
if(document.orderForm.address.value == ""){ 
document.getElementById('errors').innerHTML = "Please Enter a address"); 
document.orderForm.address.focus(); 
return (false); 
} 
if(document.orderForm.city.value == ""){ 
document.getElementById('errors').innerHTML = "Please Enter a City"); 
document.orderForm.city.focus(); 
return (false); 
} 
if(document.orderForm.postalCode.value == "" || 
document.orderForm.postalCode.value.length != 6){ 
document.getElementById('errors').innerHTML = "Please Enter a correct PostalCode"); 
document.orderForm.postalCode.focus(); 
return (false); 
} 
if(document.orderForm.province.value == "Select"){ 
document.getElementById('errors').innerHTML = "Please Select your province") 
return (false); 
} 
if(document.orderForm.widget1qty.value == "0" || document.orderForm.widget1qty.value == "" && 
document.orderForm.widget2qty.value == "0" || document.orderForm.widget2qty.value == "" && 
document.orderForm.widget2qty.value == "0" || document.orderForm.widget2qty.value == ""){ 
document.getElementById('errors').innerHTML = "Please Select at least one item") 
return (false); 
} 
else 
{ 
return(true); 
} 
} 
</script> 



    <form name="orderForm" method="POST" action="processForm.html" onSubmit="return validate();"> 
+0

看看這個[鏈接](http://www.w3schools.com/js/js_form_validation.asp)用於表單驗證。 – 2014-10-28 05:23:59

+0

你的validate函數中有幾個語法錯誤,比如'document.getElementById('errors')。innerHTML =「請輸入一個地址」);',在這個結尾處有括號。 – Ravi 2014-10-28 05:32:29

+0

修正了錯誤,仍然不能正常工作 – 2014-10-28 05:41:18

回答

2

參照此示例代碼,絕對解決你的問題

<html> 
<head> 
<title>Form Validation</title> 
<script type="text/javascript"> 
<!-- 
function validate() 
{ 
    if(document.myForm.Name.value == "") 
    { 
     document.getElementById("errors").innerHTML = "Please enter first name"; 
    document.myForm.Name.focus() ; 
    return false; 
    } 
    return(true); 
} 
//--> 
</script> 
</head> 
<body> 
<form action="processForm.html" name="myForm" onsubmit="return(validate());"> 
<table cellspacing="2" cellpadding="2" border="1"> 
<tr> 
    <td align="right">Name</td> 
    <td><input type="text" name="Name" /></td> 

<td><input type="submit" value="Submit" /></td> 
</tr> 
</table> 
</form> 
<div id="errors"></div> 
</body> 
</html> 
+0

真棒它的工作...非常感謝你的幫助 – 2014-10-28 07:46:31

+0

唯一的事情是,它不顯示錯誤的列表,它只顯示一次的錯誤。就好像它的第一個名字不存在,它會顯示firstname錯誤,然後它會轉到姓氏以檢查它是否丟失,你知道如何在頁面上顯示錯誤的完整列表嗎? – 2014-10-28 08:10:04

+0

好吧等待我會盡快回復您 – soundhiraraj 2014-10-28 08:15:16

-2

請使用下面的代碼。它工作正常。

function validate() { 
    if (document.orderForm.firstName.value=="") { 
    document.getElementById("errors").innerHTML = `Please enter first name`; 
    document.getElementById("firstName").focus(); 
    return (false); 
    } 
} 


<form name="orderForm" onSubmit="return validate();"> 
    <input type="text" id="firstName" /> 
    <input type="submit" id="btnSubmit" value="submit" /> 
    <div id="errors"></div> 
</form> 

http://www.sharepointprog.com

+0

這與OP的發佈樣本有什麼不同? – 2014-10-28 06:17:07

+0

它仍然提交從成功 – 2014-10-28 06:22:07