使用Jquery.validate.js验证电子邮件地址的格式 [英] Validate Format of an Email Address using Jquery.validate.js

查看:790
本文介绍了使用Jquery.validate.js验证电子邮件地址的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jquery.validate.js 来验证公式编制器的电子邮件输入. /p>

它运作良好,但是我很惊讶它是有效的电子邮件,但格式如下:name @ domain

我认为缺少.TLD,不是吗?

我检查了jquery.validate.js文件中的功能.他们从

验证表单的功能如下:

$("#subscription-form").validate({
});

我需要包括什么,所以脚本使用我的自定义方法而不是电子邮件方法?

尝试以下Regex:

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

I'm using jquery.validate.js to validate the Email Input of the Formular.

It's working well, but I'm suprised that it's excepting emails as valid with the following format: name@domain

In my opinion there is missing a .TLD, no?

I checked the function inside the jquery.validate.js file. They are referencing the specification from www.whatwg.org for a valid email Adress. On the site it says that a valid email address should have the format name@doman.tld

The JS Regex inside jquery.validate.js is the following:

email: function( value, element ) {
    return this.optional( element ) || /^[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])?)*$/.test( value );
},

How do I use a custom Regex that validates if the email includes a .TLD?

I specified the custom validation method as following:

jQuery.validator.addMethod("customemail", function(value, element) {
    return this.optional(element) || /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value);
}, "Please enter a valid email address.");

The function to validate the form is the following:

$("#subscription-form").validate({
});

What do I need to include, so the script uses my custom method instead of the email method?

解决方案

Try this Regex:

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

这篇关于使用Jquery.validate.js验证电子邮件地址的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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