我是一个非常正则表达式的菜鸟,但这很简单,我无法理解为什么当我在jquery验证插件中使用特殊字符(如引号或小数点放在字符串之前)来验证表单时,为什么允许使用特殊字符。我需要输入6个字符(3个字母然后3个数字)
这是代码
$(document).ready(function()
{
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
$('#taxistep1').validate({
rules:{
'targa':{
required: true,
regex: "[A-Za-z]{3}[0-9]{3}"
}
},
messages:{
'targa':{
required: "Inserire il numero di targa",
regex: "Formato targa non valido"
}
}
});
});
最佳答案
尝试分别使用start(^
)和end($
)锚点来匹配输入字符串的开头和结尾:
regex: "^[A-Za-z]{3}[0-9]{3}$"
这将确保在输入的匹配部分之前或之后没有多余的字符出现。
关于jquery - 如何从正则表达式中排除特殊字符?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20021141/