我正在构建一些验证但是当我提交正确的输入时它仍显示为无效,无法弄清楚我哪里出错了!
该功能适用于无效输入但不适用于有效输入
使用Javascript / jQuery的
(function(){
var form = '.contact-form',
alert = '.alert',
fieldWrap = '.control-group',
errorMsg = '.error-message',
error = 'error',
show = 'show',
hide = 'hide';
var textField = function(){$(field).val() !== (regEx); return false};
var select = function(){$(field + ' option:selected').val() === regEx; return false};
var validate = function(ifParam,field,regEx,e){
if(ifParam()){
$(field).parents(fieldWrap).addClass(error).find(errorMsg).addClass(show);
return false;
}
else{
$(field).parents(fieldWrap).removeClass(error).find(errorMsg).addClass(hide);
return true;
}
};
var valid = function(e){
validate(textField,'#first-name' ,/^[a-zA-Z]$/,e);
validate(textField,'#last-name' ,/^[a-zA-Z]$/,e);
validate(textField,'#contact-number' ,/^[a-zA-Z]$/,e);
validate(textField,'#email' ,/^[a-zA-Z]$/,e);
validate(textField,'#landline-number',/^[a-zA-Z]$/,e);
validate(textField,'#postcode' ,/^[a-zA-Z]$/,e);
validate(select ,'#title' ,'select',e);
return true;
};
$(document).on('submit',form,function(e){
if(!valid()){
$(alert).addClass(show);
e.preventDefault();
e.stopPropagation();
}
else{
$(this).unbind('submit').submit();
}
});
})();