Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript表单验证在Chrome中有效,但在IE或Firefox中无效_Javascript_Forms_Validation - Fatal编程技术网

Javascript表单验证在Chrome中有效,但在IE或Firefox中无效

Javascript表单验证在Chrome中有效,但在IE或Firefox中无效,javascript,forms,validation,Javascript,Forms,Validation,我已经为一个注册表单编写了一个客户端表单验证脚本,它在Google Chrome中运行良好,但在Firefox或Internet Explorer中无法验证 以下是整个脚本: <!--- Registration Form Validation ---> <script type="text/javascript"> //Validate radio buttons function checkRadioArray(radioButtons){ for (v

我已经为一个注册表单编写了一个客户端表单验证脚本,它在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 --->

//验证单选按钮
功能检查RadioArray(单选按钮){
对于(var k=0;k0);
}
//验证登记表
函数验证(表单){
//设置变量
var错误=[];
var roommate=form.roommate.value;
var房间=form.room.value;
var satses=form.satses.value;
var SunSess=form.SunSess.value;
if(!checkRadioArray(form.RegType)){
错误[errors.length]=“请选择您的注册类型。”
}
if(!checkRadioArray(form.FirstTimer)){
errors[errors.length]=“请说明这是否是您第一次参加此会议。”;
}
if(!checkRadioArray(form.Tshirt)){
错误[errors.length]=“请指出您的T恤尺寸。”;
}
如果(!checkRadioArray(表格室)){
错误[errors.length]=“请指明您的房间选择。”;
}
如果(房间=“双人房”){
如果(!检查长度(室友)){
错误。推送(“您选择了一个双人房间,请指明室友。”);
}
}
如果(卫星!=“关闭”){
如果(!选中选择(表单Sess1_1)){
错误[errors.length]=“请指出会话1的第一选择。”;
}
}
如果(卫星!=“关闭”){
如果(!选中选择(表单Sess1_2)){
错误[errors.length]=“请指出会话1的第二选择。”;
}
}  
如果(卫星!=“关闭”){
如果(!选中选择(表Sess2_1)){
错误[errors.length]=“请指出您在第2次会话中的第一选择。”;
}
}
如果(卫星!=“关闭”){
如果(!选中选择(表单Sess2_2)){
错误[errors.length]=“请指出会话2的第二选择。”;
}
}  
如果(卫星!=“关闭”){
如果(!选中选择(表Sess3_1)){
错误[errors.length]=“请指出您在第3次会话中的第一选择。”;
}
}
如果(卫星!=“关闭”){
如果(!选中选择(表单Sess3_2)){
错误[errors.length]=“请指出第三次会话的第二个选择。”;
}
}  
如果(卫星!=“关闭”){
如果(!选中选择(表格Sess4_1)){
errors[errors.length]=“请指出您在第4课时的第一选择。”;
}
}
如果(卫星!=“关闭”){
如果(!选中选择(表格Sess4_2)){
errors[errors.length]=“请指出您在会话4中的第二个选择。”;
}
}
如果(阳光!=“关闭”){
如果(!选中选择(表格Sess5_1)){
错误[errors.length]=“请指出您在第5课时的第一选择。”;
}
} 
如果(阳光!=“关闭”){
如果(!选中选择(表格Sess5_2)){
错误[errors.length]=“请指出第5次会话的第二选择。”;
}
}
如果(阳光!=“关闭”){
如果(!选中选择(表格Sess6_1)){
错误[errors.length]=“请指出您在第6课时的第一选择。”;
}
}
如果(阳光!=“关闭”){
如果(!选中选择(表格Sess6_2)){
错误[errors.length]=“请指出您在第6课时的第一选择。”;
}
}
如果(errors.length>0){
报告错误(错误);
返回false;
}
返回true;
}
由于某些原因,if语句…..if(satses!=“off”)和if(SunSess!=“off”)在Firefox或IE中不会验证为true,但在Chrome中会验证为true??当我尝试……时,我也遇到了同样的问题。。。。如果(房间=“双人房”)


我做错了什么???

您是否为表单元素指定了
name
属性,或者只指定了
id
s?FF/IE认为
satses
的值是什么?是的,所有表单元素都有名称和id。当我转储表单值时,FF认为satses=on。这就是为什么我不明白为什么我尝试时它不会验证…如果(satses!=“off”)或…如果(satses==“on”)…这两个都不会在FF或IE中验证??好的,我想我找到了答案…如果(document.getElementById('EnableSatSess')。选中)…其中EnableSatses是该单选按钮的ID,似乎在所有浏览器中都有效。请回答您自己的问题。否则,它将保留在“未回答问题”列表/选项卡上。