Javascript表单验证在Chrome中运行,但不适用于IE或Firefox

时间:2014-04-25 17:23:16

标签: javascript forms validation

我为注册表单编写了一个客户端表单验证脚本,该脚本在Google Chrome中运行良好,但在Firefox或Internet Explorer中无法验证?

以下是整个脚本:

<!--- Registration Form Validation --->
<script type="text/javascript">

//Validate radio buttons
  function checkRadioArray(radioButtons){
    for (var k=0; k < radioButtons.length; k++) {
        if (radioButtons[k].checked) {
            return true;
        }
    }
    return false;
}

//Initialize error messages
  function reportErrors(errors) {
      var msg = "There were some problems with your form submission. Please correct the errors listed below \n";
      var numError;
      for (var j=0; j<errors.length; j++) {
         numError = j +1;
         msg += "\n" + numError + ". " + errors[j];
      }
       alert(msg);
  }

  //Validate text fields
  function checkLength(text, min, max){
    min = min || 1;
    max = max || 10000;

    if (text.length < min || text.length > max) {
        return false;
    }
    return true;
}

//Validate select menus
function checkSelect(select){
    return (select.selectedIndex > 0);
}

//Validate Registration Form
  function validate(form){

    //Set Variables
    var errors = [];
    var roommate = form.Roommate.value;
    var room = form.Room.value;
    var SatSess = form.SatSess.value;
    var SunSess = form.SunSess.value;


    if ( !checkRadioArray(form.RegType) ) {
      errors[errors.length] = "Please select your registration type."
    }

    if ( !checkRadioArray(form.FirstTimer) ) {
       errors[errors.length] = "Please indicate if this is your first time attending this conference.";
    }

    if ( !checkRadioArray(form.Tshirt) ) {
       errors[errors.length] = "Please indicate your Tshirt size.";
    }

     if ( !checkRadioArray(form.Room) ) {
       errors[errors.length] = "Please indicate your room choice.";
     }

     if (room == "Double")  {

         if ( !checkLength(roommate) ) {
       errors.push("You selected a double room, please indicate a roommate.");
     }
    }

    if (SatSess != "off") {
       if( !checkSelect(form.Sess1_1) ) {
        errors[errors.length] = "Please indicate your first choice for Session 1.";
      }
    }

    if (SatSess != "off") {
        if ( !checkSelect(form.Sess1_2) ) {
        errors[errors.length] = "Please indicate your second choice for Session 1.";
    }
   }  

   if (SatSess != "off") {
       if( !checkSelect(form.Sess2_1) ) {
        errors[errors.length] = "Please indicate your first choice for Session 2.";
      }
    }

    if (SatSess != "off") {
        if ( !checkSelect(form.Sess2_2) ) {
        errors[errors.length] = "Please indicate your second choice for Session 2.";
    }
   }  

   if (SatSess != "off") {
       if( !checkSelect(form.Sess3_1) ) {
        errors[errors.length] = "Please indicate your first choice for Session 3.";
      }
    }

    if (SatSess != "off") {
        if ( !checkSelect(form.Sess3_2) ) {
        errors[errors.length] = "Please indicate your second choice for Session 3.";
    }
   }  

   if (SatSess != "off") {
       if( !checkSelect(form.Sess4_1) ) {
        errors[errors.length] = "Please indicate your first choice for Session 4.";
      }
    }

    if (SatSess != "off") {
        if ( !checkSelect(form.Sess4_2) ) {
        errors[errors.length] = "Please indicate your second choice for Session 4.";
    }
   }

  if (SunSess != "off") {
        if ( !checkSelect(form.Sess5_1) ) {
        errors[errors.length] = "Please indicate your first choice for Session 5.";
    }
   } 

   if (SunSess != "off") {
        if ( !checkSelect(form.Sess5_2) ) {
        errors[errors.length] = "Please indicate your second choice for Session 5.";
    }
   }

   if (SunSess != "off") {
        if ( !checkSelect(form.Sess6_1) ) {
        errors[errors.length] = "Please indicate your first choice for Session 6.";
    }
   }

   if (SunSess != "off") {
        if ( !checkSelect(form.Sess6_2) ) {
        errors[errors.length] = "Please indicate your first choice for Session 6.";
    }
   }


     if (errors.length > 0) {
       reportErrors(errors);
       return false;
     }
         return true;

  }


</script>

<!--- End Registration Form Validation --->

出于某种原因if语句.....如果(SatSess!=&#34; off&#34;)和if(SunSess!=&#34; off&#34;)不会被验证为在Firefox或IE中是真的,但它们会在Chrome中?当我尝试时,我遇到了同样的问题....如果(房间==&#34; Double&#34;)。

我做错了什么?

0 个答案:

没有答案