2013-05-28 112 views
0

我的EnquiryForm上有一些必填字段。我確實有一個提示框,提示用戶錯誤。但我已經刪除它並輸入文本字段中的錯誤。查詢表格未提交

現在我不確定如何在所有條件都滿足時編寫返回false。

驗證工作正常,表單只是不提交。

我是一個新手,以提前JavaScript編程使appologies .....

這裏是我的代碼:

// Enquiry Form Validation 
function enquiryFormValidation() { 

// Setting Variables 

var errormessage = ""; 

var ufirstname = document.getElementById("firstname"); 
var usurname = document.getElementById("surname"); 
var uaddress = document.getElementById("address"); 
var upostcode = document.getElementById("postcode"); 
var uemail = document.getElementById("email"); 
var uhearabout = document.getElementById("where"); 
var fday = document.getElementById("fday"); 
var fmonth = document.getElementById("month"); 
var fyear = document.getElementById("year"); 
var ftype = document.getElementById("ftype"); 
var garrival = document.getElementById("garrival"); 
var fend = document.getElementById("fend"); 


if (ufirstname.value == "(e.g. John)") { 
ufirstname.style.border = "1px solid red" ; 
ufirstname.style.color = "red" ; 
ufirstname.value = "Firstname Required" 
} 

if (usurname.value == "(e.g. Smith)") { 
usurname.style.border = "1px solid red" ; 
usurname.style.color = "red" ; 
usurname.value = "Surname Required" 
} 

if (uaddress.value == "(e.g. 101 Party Street, Wellington)") { 
uaddress.style.border = "1px solid red" ; 
uaddress.style.color = "red" ; 
uaddress.value = "Address Required" 
} 

if (upostcode.value == "(e.g. TF1 7HU)") { 
upostcode.style.border = "1px solid red" ; 
upostcode.style.color = "red" ; 
upostcode.value = "Postcode Required" 
} 

if (uemail.value == "(e.g. [email protected])") { 
uemail.style.border = "1px solid red" ; 
uemail.style.color = "red" ; 
uemail.value = "Email Address Required" 
} 

if (uhearabout.options[uhearabout.selectedIndex].value == "blank") { 
uhearabout.style.border = "1px solid red" ; 
uhearabout.style.color = "red" ; 
} 

if (fday.options[fday.selectedIndex].value == "blank") { 
fday.style.border = "1px solid red" ; 
fday.style.color = "red" ; 
} 

if (fmonth.options[fmonth.selectedIndex].value == "blank") { 
fmonth.style.border = "1px solid red" ; 
fmonth.style.color = "red" ; 
} 

if (fyear.options[fyear.selectedIndex].value == "blank") { 
fyear.style.border = "1px solid red" ; 
fyear.style.color = "red" ; 
} 

if (ftype.options[ftype.selectedIndex].value == "blank") { 
ftype.style.border = "1px solid red" ; 
ftype.style.color = "red" ; 
} 

if (garrival.options[garrival.selectedIndex].value == "hh:mm") { 
garrival.style.border = "1px solid red" ; 
garrival.style.color = "red" ; 
} 

if (fend.options[fend.selectedIndex].value == "hh:mm") { 
fend.style.border = "1px solid red" ; 
fend.style.color = "red" ; 
} 

{ 
return false;  
} 

} // End of enquiryForm-validation function 
+0

你得到任何錯誤消息? – ajtrichards

+0

根據你如何調用該函數,它應該返回'false'給_stop_提交,並且返回'true'給_allow_提交。因此,可能需要添加一個標誌'formValid = true',然後在每個if語句內添加'formValid = false',然後在函數結尾處使用'return formValid;'。 – nnnnnn

+0

不,沒有錯誤消息。驗證正在工作,我將添加 – user2428136

回答

0

您需要重構這一點。

例如

var valFields = { 
    "firstname":"Please enter a first name", 
    "surname":"Please enter a surname", 
    . 
    . 
    "fend":"please enter a valid time in HH:mm format" 
}// note no comma after the last 

function validate() { 
    for (var fieldID in valFields) { 
    var field = document.getElementById(fieldID); 
    field.className ="normal"; // for example 
    if (
     (field.type=="text" && field.value==field.defaultValue) || 
     (field.type=="select" && field.selectedIndex<1) // and so on 
    ) { 
     field.className="red" // for example 
     alert(valFields[fieldID]); 
     return false;// cancel submit 
    } 
    } 
    return true;// allow submit 
} 

要使用

<form onsubmit="return validate()"> 

這應該真正在頭部,而不是內嵌設置,但我在iPad上,累了打字的