我已经创建了一个自定义jquery验证,我的验证总是失败(总是返回true),我花了几个小时但却无法弄清楚我的代码出了什么问题:
var isvalid = [];
var counterrors = isvalid.length;
function salutationvalidation(){
var salutation = $("#salutation option").attr("selected");
var salutation_error = $("#salutation").parent(".form-input").siblings(".form-info").children(".1stperror");
if(salutation!=undefined){
$(salutation_error).css("display","table-cell");
isvalid.push(salutation_error);
}else{
$(salutation_error).css("display","none");
isvalid.pop(salutation_error);
}
}
function namevalidation(){
var checkname = $("#first-name").val();
var namevalid = new RegExp("(^[a-zA-Z'-]+$)");
var name_error = $("#first-name").parent(".form-input").siblings(".form-info").children(".1stperror");
if(checkname.match(namevalid)){
$(name_error).css("display","table-cell");
isvalid.push(name_error);
}else{
$(name_error).css("display","none");
isvalid.pop(name_error);
}
}
$("#form-v2").submit(function(){
salutationvalidation();
namevalidation();
if(counterrors == 0){
return true;
}else{
return false;
}
});
任何有关解决上述代码的帮助都会非常感激,因为我还必须满足其他4个输入字段。当我警告反击时,我得到4,有时5,6。似乎值没有清除
答案 0 :(得分:0)
试试这个......
$("#form-v2").submit(function(){
salutationvalidation();
namevalidation();
counterrors = isvalid.length;
if(counterrors == 0){
return true;
}else{
return false;
}
});