Javascript验证错误

Javascript验证错误,javascript,Javascript,请想象一下web表单的这一部分。两个同名的复选框=myCheckbox和两个文本输入。如果选中第一个复选框,则第一个文本输入不得为空。如果选中第二个复选框,则第二个文本输入不得为空。为了验证这一点,我使用以下脚本: // huge validation code above }else if (!document.myForm.myCheckbox[0].checked && !document.myForm.myCheckbox[1].checked){ jA

请想象一下web表单的这一部分。两个同名的复选框=myCheckbox和两个文本输入。如果选中第一个复选框,则第一个文本输入不得为空。如果选中第二个复选框,则第二个文本输入不得为空。为了验证这一点,我使用以下脚本:

// huge validation code above   

}else if (!document.myForm.myCheckbox[0].checked && !document.myForm.myCheckbox[1].checked){ 
    jAlert ('Please select one of the two checkboxes!',function(){$(myForm.myCheckbox).focus();}); 
    return false;   

}else if (myForm.myCheckbox[0].checked && myForm.myFirstTextInput.value=="") {
    jAlert ('You have selected the first checkbox. Please make sure that the first text input is not empty!',function(){$(myForm.myFirstTextInput).focus();});
    return false;

}else if (myForm.myCheckbox[1].checked && myForm.mySecondTextInput.value=="") {
    jAlert ('You have selected the second checkbox. Please make sure that the second text input is not empty!',function(){$(myForm.mySecondTextInput).focus();});
    return false;

// huge validation code below   
一切正常。但是现在,我还要检查用户在第一个文本输入中是否输入了至少10位数字。我更新了我的验证代码,请参见以下内容:

// huge validation code above   

}else if (!document.myForm.myCheckbox[0].checked && !document.myForm.myCheckbox[1].checked){ 
    jAlert ('Please select one of the two checkboxes!',function(){$(myForm.myCheckbox).focus();}); 
    return false;   

}else if (myForm.myCheckbox[0].checked && myForm.myFirstTextInput.value=="") {
    jAlert ('You have selected the first checkbox. Please make sure that the    first text input is not empty!',function(){$(myForm.myFirstTextInput).focus();});
    return false;

}else if (myForm.myCheckbox[0].checked && myForm.myFirstTextInput.value!=="")
var x = document.getElementById("myFirstTextInput");
var y = x.value;
var totalNrOfDigits = 0;
for(var i = 0; i < y.length; i++){
if(/\d/.test(y[i])){
totalNrOfDigits++;
}
}
if(totalNrOfDigits < 10) {
jAlert ('Please make sure that the first text input contains at least 10 digits!',function(){$(myForm.myFirstTextInput).focus();});
return false;   


}else if (myForm.myCheckbox[1].checked && myForm.mySecondTextInput.value=="") {
    jAlert ('You have selected the second checkbox. Please make sure that the second text input is not empty!',function(){$(myForm.mySecondTextInput).focus();});
    return false;

// huge validation code below   
我的问题是:在我添加至少10位数字的复选框后,如果用户单击第二个复选框,那么mySecondTestInput将不再有效,表单将被提交。我真的不明白为什么已经浪费了五一节的大部分时间


抱歉,因为我没有添加全部代码:它非常庞大,是一个非常复杂的web表单

在第二个代码示例中,您忘记了第11行的左括号,因此只计算else if条件后面的第一条语句

}else if (myForm.myCheckbox[0].checked && myForm.myFirstTextInput.value!=="")
                                                                              ^
                                                                              |

编辑了我的代码,最初添加了一个额外的}。对不起,不是这样,但是谢谢你。