该计划缺少什么?即使字段留空,表单也会被接受

时间:2015-08-26 08:34:36

标签: javascript html forms validation

    <form name="form" method="post" action="thanks.html" onsubmit="final()">
            <div id="errorBox"> </div>
            <div class="marginn">                               
                                                <input type="name" name="name" value="" placeholder="name" size="40" /></br>
                                                <input type="email" name="email" placeholder="email" size="40" value="" /></br>



            <div><b>Birthday</b>
            <select name="birthday_month" >
              <option value="" selected >Month</option>
              <option value="1">Jan</option>
              <option value="2">Feb</option>
              <option value="3">Mar</option>
              <option value="12">Dec</option>
            </select>
              <select name="birthday_day" >
              <option value="" selected>Day</option>
              <option value="1">1</option>
        </select>
            &nbsp;&nbsp;
            <select name="birthday_year">
              <option value="" selected>Year</option>
              <option value="2009">2009</option>
            </select>
        </div>
    <input type="submit" name="submit" value="submit"/>
    </form>


\\the javascript code for validation is 

使用下面给出的javascript代码验证上述html表单。即使字段填写不正确或留空,表单也会提交。 请告诉我缺少什么。 提前致谢。 :)

function final(){
 var emailRegex = /^[A-Za-z0-9._]*\@[A-Za-z]*\.[A-Za-z]{2,5}$/;
 var name = document.form.name.value;
 var femail = document.form.email.value;
 var fmonth = document.form.birthday_month.value;
   var fday = document.form.birthday_day.value;
   var fyear = document.form.birthday_year.value;


 if( name == null || name== "")
   {
     document.form.name.focus() ;
     //document.getElementById("errorBox").innerHTML = "enter the first name";
     alert("Name Empty");
     return false;
   }
   if (femail == null || femail== "" )
 {
  document.form.email.focus();
  document.getElementById("errorBox").innerHTML = "enter the email";
  alert("please enter Email");
  return false;
  }
  else if(!emailRegex.test(femail)){
  document.form.email.focus();
  document.getElementById("errorBox").innerHTML = "enter the valid email";
  return false;
  }
   if (fmonth == null|| fmonth=="") {
        document.form.birthday_month.focus();
  document.getElementById("errorBox").innerHTML = "select the birthday month";
        return false;
     }
  if (fday == ""|| fday== null) {
        document.form.birthday_day.focus();
  document.getElementById("errorBox").innerHTML = "select the birthday day";
        return false;
     }
  if (fyear == ""||fyear==null) {
        document.form.birthday_year.focus();
  document.getElementById("errorBox").innerHTML = "select the birthday year";
        return false;
     }
  if(document.form.radiobutton[0].checked == false && document.form.radiobutton[1].checked == false){
    document.getElementById("errorBox").innerHTML = "select your gender";
    return false;
   }

  if(name != '' && femail != ''){
      alert("done");
   document.getElementById("errorBox").innerHTML = "form submitted successfully";
  return true;
   }

}

0 个答案:

没有答案