0

我的 EnquiryForm 上有一些必填字段。我确实有一个警告框提示用户错误。但我已经删除了它并在文本字段中输入了错误。

现在我不确定在满足所有条件后如何编码 return false 。

验证工作正常,表单只是不提交。

我是javascripting的新手,所以提前申请.....

这是我的代码:

// 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. lets_party@glimmer-nights.co.uk)") {
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
4

1 回答 1

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 上并且厌倦了打字

于 2013-05-28T20:37:20.650 回答