使用jQuery和RegEx进行多语言电子邮件地址验证 [英] Multilingual email address validation with jQuery and RegEx

查看:124
本文介绍了使用jQuery和RegEx进行多语言电子邮件地址验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些jQuery和RegEx代码,可以很好地验证电子邮件地址 ...,只要该地址基于简单的拉丁字符即可.但是,当我们插入更复杂的多语言电子邮件地址时,使用本地HTML5验证和基于正则表达式的验证都会使我们的检查失败.

这是我们用于测试的中文电子邮件地址:

伊昭杰@邮件.商务

这是JS验证代码(我没有费心去掉名称空间和内部实用程序方法).我们有一个隐藏的HTML5输入控件,类型为电子邮件",我们将电子邮件地址传递给该控件,并让浏览器发挥作用.否则,我们将使用正则表达式.

我们有什么选择?似乎无法使用本机(例如基于浏览器的)验证.

um.utils.isValidEmail = function (sEmail) {
    var r = false;
    var $emailTester = {};
    var emailRegex;
    //-----

    if (Modernizr.inputtypes.email === true) {
        // Defer to native HTML5 email validation using a hidden <input type='email'> control
        $emailTester = $("#idEmailTester");
        um.utils.assertSize($emailTester);

        $emailTester.val(sEmail);
        r = $emailTester[0].checkValidity();
    } else {
        // Use a regular expression to do email validation
        // Attribution http://www.regular-expressions.info/email.html
        emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?\^_`{|}~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$/;
        r = emailRegex.test(sEmail);
    }

    return r;
};

解决方案

有一种非常简单的方法,可以将所有RegEx逻辑(一个人可以很容易地用英语应用)应用到使用Unicode的任何语言.

要匹配所有字母[A-Za-z]之类的Unicode字符,我们可以使用

[\ u0041- \ u005A],其中 \ u0041 A 的十六进制代码,而 \ u005A 的十六进制代码Z

'matchCAPS leTTer'.match(/[\u0041-\u005A]+/g)
//output ["CAPS", "TT"]

以相同的方式,我们可以根据unicode.org提供的其他十六进制顺序(例如:\ u0A10到\ u0A1F)使用其他Unicode字符或等效的十六进制代码

尝试: [电触]

如果unicode.org按此顺序提供,它将匹配电和触之间的所有字符

我不懂中文:)

I have some jQuery and RegEx code that works great validating email addresses...as long as the address is based on simple Latin characters. However, when we plug in more complex multilingual email addresses, our checks fail using both native HTML5 validation and validation based on a Regular Expression.

Here's the Chinese email address we're using for testing:

伊昭傑@郵件.商務

And here's the JS validation code (I haven't bothered to strip out namespaces and internal utility methods). We have a hidden HTML5 input control of type "email", and we pass the email address to that control and let the browser work its magic. Otherwise, we use a regular expression.

What are our options? Seem like using native (e.g. browser-based) validation just won't work.

um.utils.isValidEmail = function (sEmail) {
    var r = false;
    var $emailTester = {};
    var emailRegex;
    //-----

    if (Modernizr.inputtypes.email === true) {
        // Defer to native HTML5 email validation using a hidden <input type='email'> control
        $emailTester = $("#idEmailTester");
        um.utils.assertSize($emailTester);

        $emailTester.val(sEmail);
        r = $emailTester[0].checkValidity();
    } else {
        // Use a regular expression to do email validation
        // Attribution http://www.regular-expressions.info/email.html
        emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?\^_`{|}~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$/;
        r = emailRegex.test(sEmail);
    }

    return r;
};

解决方案

There is a very simple method to apply all you RegEx logic(that one can apply easily in English) for any Language using Unicode.

For matching a range of Unicode Characters like all Alphabets [A-Za-z] we can use

[\u0041-\u005A] where \u0041 is Hex-Code for A and \u005A is Hex Code for Z

'matchCAPS leTTer'.match(/[\u0041-\u005A]+/g)
//output ["CAPS", "TT"]

In the same way we can use other Unicode characters or their equivalent Hex-Code according to their Hexadecimal Order (eg: \u0A10 to \u0A1F) provided by unicode.org

Try: [电-触]

It will match all characters between 电 and 触 if provided by unicode.org in this order

I don't know chinese :)

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

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