我之前见过这个问题。但是我还没有能够提出解决方案。我的表单不会在jsfiddle上验证它给我的shell表单错误任何想法?
提前致谢!
var error_count = 0;
//main validation function
function validation(form){
error_count = 0;
var personalId = form.personal_id.value;
var firstName = form.first_name.value;
var lastName = form.last_name.value;
var radio_length = form.sex.length;
var checked="";
//check personal ID if empty
if(personalId == "" ){
error_code("personal_id");
}
else{
resolved_code("personal_id");
}
//checks personal ID so only numbers are entered
if(!personalId.match(/^[0-9]+$/))
{
error_code("personal_id");
}
//checks personal ID for 7 digits
if(personalId.length < 7)
{
error_code("personal_id");
}
else{
resolved_code("personal_id");
}
//checks first name input if empty
if(firstName == "" ){
error_code("first_name");
}
else{
resolved_code("first_name");
}
// if(firstName.match(/\s/){
// document.getElementById("first_name").style.backgroundColor = "#FF4D4D";
// error_count += 1;
// }
//check last name input if empty
if(lastName == "" ){
error_code("last_name");
}
else{
resolved_code("last_name");
}
//check to see if either radio button is checked
for(i=0; i<radio_length; i++)
{
if(form.sex[i].checked)
{
checked = form.sex[i].value;
}
}
if(checked == "")
{
error_count++
alert("Gender wasnt selected");
}
//functions to include code for error or resolved cases
function error_code(elem){
document.getElementById(elem).style.backgroundColor = "#FF4D4D";
error_count ++;
}
function resolved_code(elem){
document.getElementById(elem).style.backgroundColor = "white";
}
if(error_count == 0){
return true;
}
else{
return false;
}