覆盖jQuery验证插件电子邮件地址验证 [英] override jquery validate plugin email address validation

查看:101
本文介绍了覆盖jQuery验证插件电子邮件地址验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现jQuery验证插件regex不足以满足我的要求.它接受任何电子邮件地址xxx@hotmail.x作为有效的电子邮件地址,而我希望能够提供此正则表达式/^([a-zA-Z0-9_.-+])+@@(([[a-zA -Z0-9-])+.)+([[a-zA-Z0-9] {2,4})+ $/,以便验证地址的完整.com部分.我更关心的是能够提供自己的正则表达式而不是简单的正则表达式(因为没有用于电子邮件验证的简单正则表达式)

I find that jQuery validation plugin regex to be insufficient for my requirement. It accepts any email address xxx@hotmail.x as a valid email address whereas I want to be able to supply this regex /^([a-zA-Z0-9_.-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/ so that it would validate complete .com part of the address. I'm more concerned about being able to supply my own regex than getting a fool proof regex(as there is no fool proof regex for email validation)

仅供参考:我也在做服务器端验证,但是现在我不担心哪个正则表达式是正确的电子邮件地址.

Just FYI: I'm also doing server side validation but at this point I'm not worried about which email address regex is right.

在jQuery validate plugin的"rules"部分中有没有办法做到这一点?

Is there a way to do that within the "rules" section of jQuery validate plugin?

这是我现在的规则部分:

This is my rules section right now:

rules: {
                        email: {
                            required:  {
                                    depends:function(){
                                        $(this).val($.trim($(this).val()));
                                        return true;
                                    }   
                                },
                            email: true
                        },

推荐答案

我不会这样做,但是为了得到答案,您需要添加自己的自定义验证.

I wouldn't do this but for the sake of an answer you need to add your own custom validation.

//custom validation rule
$.validator.addMethod("customemail", 
    function(value, element) {
        return /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
    }, 
    "Sorry, I've enabled very strict email validation"
);

然后在您的规则中添加:

Then to your rules add:

rules: {
                    email: {
                        required:  {
                                depends:function(){
                                    $(this).val($.trim($(this).val()));
                                    return true;
                                }   
                            },
                        customemail: true
                    },

这篇关于覆盖jQuery验证插件电子邮件地址验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆