我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。


当前回答

以下是 Regex 认证:

在 @ (-) 和 (.) 之前的空间字符不应该聚集在一起 之后 @ 没有特殊字符 之后 @ 2 字符 必须在 @ 电子邮件 长度 之前 应该 少于 128 字符 函数 验证 电子邮件(电子邮件) { var chrbeforAt = email.substr(0, email.indexOf('@')); 如果(!($.trim(电子邮件) 长度 > 127)) { 如果(chrbeforAt.length >= 2) { var re = /^([^<>()[\]{}'^?

其他回答

我正在使用这个功能

/**
 * @param {*} email
 */
export const validateEmail = email => {
    return new RegExp(/[\w-]+@([\w-]+\.)+[\w-]+/gm).test(email);
};

一个不检查 TLD 的存在的解决方案是不完整的。

几乎所有这些问题的答案都建议使用Regex来验证电子邮件地址,我认为Regex仅适用于基本验证,似乎检查电子邮件地址的验证实际上是两个单独的问题:

电子邮件格式验证:确保电子邮件是否符合 RFC 5322 的电子邮件格式和模式,以及 TLD 是否实际存在。

例如,虽然地址 example@example.ccc 将通过 regex,但它不是有效的电子邮件,因为 ccc 不是 IANA 的顶级域名。

2、确保电子邮件实际存在:这样做,唯一的选择是向用户发送电子邮件。

有一件事你必须明白,第二件事你决定使用一个常规的表达式来验证电子邮件:这可能不是一个好主意,一旦你达成协议,有很多实施,可以让你在那里半路,这篇文章会把它们合成好。

简而言之,但是,唯一的方式是绝对的,肯定的是,用户输入的是实际上一个电子邮件是实际上发送一个电子邮件,看看发生了什么。

<pre>
**The personal_info part contains the following ASCII characters.
1.Uppercase (A-Z) and lowercase (a-z) English letters.
2.Digits (0-9).
3.Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
4.Character . ( period, dot or fullstop) provided that it is not the first or last character and it will not come one after the other.**
</pre>
*Example of valid email id*
<pre>
yoursite@ourearth.com
my.ownsite@ourearth.org
mysite@you.me.net
xxxx@gmail.com
xxxxxx@yahoo.com
</pre>
<pre>
xxxx.ourearth.com [@ is not present] 
xxxx@.com.my [ tld (Top Level domain) can not start with dot "." ]
@you.me.net [ No character before @ ]
xxxx123@gmail.b [ ".b" is not a valid tld ]
xxxx@.org.org [ tld can not start with dot "." ]
.xxxx@mysite.org [ an email should not be start with "." ]
xxxxx()*@gmail.com [ here the regular expression only allows character, digit, underscore and dash ]
xxxx..1234@yahoo.com [double dots are not allowed
</pre>
**javascript mail code**

    function ValidateEmail(inputText)
    {
    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if(inputText.value.match(mailformat))
    {
    document.form1.text1.focus();
    return true;
    }
    else
    {
    alert("You have entered an invalid email address!");
    document.form1.text1.focus();
    return false;
    }
    }

微软在 ASP.NET MVC 中提供的常规表达式是

/^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/

我在这里写的,如果它是错误的 - 尽管它一直是完美的我的需求。