使用jquery验证插件,如何在文本框中添加正则表达式验证? [英] using the jquery validation plugin, how can I add a regex validation on a textbox?
本文介绍了使用jquery验证插件,如何在文本框中添加正则表达式验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是jquery验证插件: http://bassistance.de/ jquery-plugins / jquery-plugin-validation /
I am using the jquery validation plugin from: http://bassistance.de/jquery-plugins/jquery-plugin-validation/
如何在特定文本框上添加正则表达式检查?
How can I add a regex check on a particular textbox?
我想检查以确保输入是字母数字。
I want to check to make sure the input is alphanumeric.
推荐答案
定义新的验证功能,在您要验证的字段的规则中使用它:
Define a new validation function, and use it in the rules for the field you want to validate:
$(function ()
{
$.validator.addMethod("loginRegex", function(value, element) {
return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
}, "Username must contain only letters, numbers, or dashes.");
$("#signupForm").validate({
rules: {
"login": {
required: true,
loginRegex: true,
}
},
messages: {
"login": {
required: "You must enter a login name",
loginRegex: "Login format not valid"
}
}
});
});
这篇关于使用jquery验证插件,如何在文本框中添加正则表达式验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文