Javascript 使用正则表达式的jQuery验证插件自定义方法
我需要为jQuery验证器创建一个新方法,但不知道从哪里开始 我想检查输入的电子邮件是否包括:“@specificdomain.com” 但这也是输入的最后一部分。例如@specificdomain.comChris不会这样做Javascript 使用正则表达式的jQuery验证插件自定义方法,javascript,jquery,regex,jquery-validate,Javascript,Jquery,Regex,Jquery Validate,我需要为jQuery验证器创建一个新方法,但不知道从哪里开始 我想检查输入的电子邮件是否包括:“@specificdomain.com” 但这也是输入的最后一部分。例如@specificdomain.comChris不会这样做 <script type="text/javascript"> jQuery.validator.addMethod("mustinclude", function(value, element) { return this.optio
<script type="text/javascript">
jQuery.validator.addMethod("mustinclude", function(value, element) {
return this.optional(element) || value == ?
}, "must include @specificdomain.com at the end of the text input");
$(document).ready(function(){ .....
以下几点对我很有用:
jQuery.validator.addMethod('matchDomain', function(value, element) {
var s=value;
var split = s.split('@');
var regex = /^([a-zA-Z0-9_.+-])+$/;
**var s2="allcoles.com";** //The split array is the domain excluding the @
**var optionalValue = this.optional(element);** //This is how other methods in alternativeMethods.js Validator handle this.
**//Debugging - This is useful to see visually what is happening
//alert(split[0]); // Shows the inputted username i.e chris or smokey
//alert(split[1]); // Shows the inputted domain
//alert(regex.test(split[0])); //Shows unfilled inputs problem or bad characters, true if good, false if bad
//alert(s2 == split[1]);** // Shows if the inputted domain matches variable s2, if it does we get a true
if (optionalValue) {
return optionalValue;
}
**if(regex.test(split[0]) && (s2 == split[1]))** // has to be == not equals
{
return true;
}
else
{
return false;
}
}, 'Please specify a @allcoles.com email');
嗨,谢谢你的帮助;我在addMethod中添加了它。我认为我没有正确理解返回类型,因为它工作不正常。Cheers Chrisit这是一个JQuery验证程序addMethod,我把自己弄糊涂了,因为我不确定我返回的是布尔值false还是元素。我修改了我原来的帖子,把你的code@Smokey,您应该始终从
.addMethod()
method.s2.equals(拆分[1])中的函数返回布尔值true
或false
。我猜这是一个数组,我的变量s2应该是specificdomain.com而不是@specificdomain.com吗?几乎等于不存在==是问题所在,s2拆分不需要@。但是如果没有你的帮助,我是无法实现这一点的。里面有几十个工作的。addMethod()
示例,这将是一个很好的学习方法。谢谢,我找不到该方法或任何接近它的方法,但它确实帮助我找到了我需要对元素执行的操作。请编辑以指出此代码与OP的不同之处。在编写问题和答案时,请记住SO也是未来读者的学习工具。我添加我的答案是为了帮助其他人,我知道这是一个学习工具。然而,我用粗体编辑了我的文章,指出了3个变化。很抱歉没有这样做。你的想法是对的,请不要认为我太挑剔了,但是一个简短的口头描述(或内联注释)会比在代码中添加星号要好得多。我想强调的是,解释某件事比期望有人做一个并列的比较要好。
var s="abc@specificdomain.com"; OR var s=value;
var split = s.split('@');
var regex = /^([a-zA-Z0-9_.+-])/;
var s2="@specificdomain.com";
if(regex.test(split[0]) && s2 == split[1])
return true;
else
return false;
jQuery.validator.addMethod('matchDomain', function(value, element) {
var s=value;
var split = s.split('@');
var regex = /^([a-zA-Z0-9_.+-])+$/;
**var s2="allcoles.com";** //The split array is the domain excluding the @
**var optionalValue = this.optional(element);** //This is how other methods in alternativeMethods.js Validator handle this.
**//Debugging - This is useful to see visually what is happening
//alert(split[0]); // Shows the inputted username i.e chris or smokey
//alert(split[1]); // Shows the inputted domain
//alert(regex.test(split[0])); //Shows unfilled inputs problem or bad characters, true if good, false if bad
//alert(s2 == split[1]);** // Shows if the inputted domain matches variable s2, if it does we get a true
if (optionalValue) {
return optionalValue;
}
**if(regex.test(split[0]) && (s2 == split[1]))** // has to be == not equals
{
return true;
}
else
{
return false;
}
}, 'Please specify a @allcoles.com email');