gpt4 book ai didi

javascript - jquery : validate two rules fails using regex

转载 作者:行者123 更新时间:2023-11-28 06:48:30 26 4
gpt4 key购买 nike

我正在尝试为一个小型验证系统创建一个jquery插件,所以我的函数中有这部分代码

   // prevent current form submitting
this.on('submit',function(e){
// remove classes from input when we focus on it
$(this).on('click','input,select',function(){
$(this).removeClass(settings.class);
});
// prevent form from being submited
e.preventDefault();
// find all inputs
$(this).find('input,select').each(function() {
if (!$(this).data('rules')) return;
// get rules array from the attribute
var rules = $(this).data("rules").split(" ");

// foreach rules
for(var i = 0; i < rules.length;i++){
// check if the validation is true or attach the given class to the input
if(validate( rules[i],$(this))){
// the validation is okey continue to the next rule
continue;
}else{
// the validation is false , attach the class to the input and stop the loop
$(this).addClass(settings.class);
break;
}
}
});
});
/* validation */
function validate(rule,input){
switch(rule){
case 'email': return patterns.email.test(input.val()); break;
case 'required': return patterns.required.test(input.val()); break;
case 'url': return patterns.url.test(input.val()); break;
case 'phone': return patterns.phone.test(input.val()); break;
case 'confirmed':
inputName = input.attr('name').split("_confirmation")[0];
return (input.val() == $('input[name='+inputName+']').val() ? true : false);
break;
case /^min\:[0-9]+/.test(rule):
length = rule.split('min:')[1];
return (input.val().trim() >= length ? true : false);
break;
case rule.match(/^max\:[1-9]+/)[0]:
length = rule.split('max:')[1];
return (input.val().trim().length <= length ? true : false);
break;
case rule.match(/^exact\:[1-9]+/)[0]:
length = rule.split('exact:')[1];
return (input.val().trim().length == length ? true : false);
break;
default:
return true;

}
}
/* Patterns */
var patterns = {
email:/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i,
required:/^[\s\t\r\n]*\S+/,
url:/(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/,
phone:/^[0-9]{10,10}$/,
}

当我尝试使用此验证 min:length max:length 验证两个输入时它返回rule.match(...) is null ,但是当我尝试使用 min:length 仅验证一个输入时,它可以正常工作。有什么帮助吗?

最佳答案

这是因为你的开关rule 是一个字符串,并且您的 case 语句为 regex。试试这个:-

/* validation */
function validate(rule,input){
switch(rule){
case 'email': return patterns.email.test(input.val()); break;
case 'required': return patterns.required.test(input.val()); break;
case 'url': return patterns.url.test(input.val()); break;
case 'phone': return patterns.phone.test(input.val()); break;
case 'confirmed':
inputName = input.attr('name').split("_confirmation")[0];
return (input.val() == $('input[name='+inputName+']').val() ? true : false);
break;
default:
if(/^min\:[0-9]+/.test(rule)){
length = rule.split('min:')[1];
return (input.val().trim().length >= length ? true : false);
}
if(/^max\:[1-9]+/.test(rule)){
length = rule.split('max:')[1];
return (input.val().trim().length <= length ? true : false);
}
if(/^exact\:[1-9]+/.test(rule)){
length = rule.split('exact:')[1];
return (input.val().trim().length == length ? true : false);
}
return true;
}
}

工作中Fiddle

关于javascript - jquery : validate two rules fails using regex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33188907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com