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


当前回答

以下是 Regex 认证:

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

其他回答

所有电子邮件地址都包含一个“at”(即 @)符号。

email.includes('@')

或者,如果您需要支持 IE/older 浏览器:

email.indexOf('@') > 0

即使你能完全确定一个电子邮件是否是RFC合成有效,这不会告诉你它是否属于提供它的人。

要做到这一点,请发送验证消息。

如果您正在使用 AngularJS,只需添加 type="email" 到输入元素:

https://docs.angularjs.org/api/ng/input/input%5Bemail%5D

如果没有输入元素,可以动态创建:

var isEmail = $compile('<input ng-model="m" type="email">')($rootScope.$new()).
    controller('ngModel').$validators["email"];

if (isEmail('email@gmail.com')) {
  console.log('valid');
} 

HTML5 本身有电子邮件验证,如果您的浏览器支持 HTML5,则可以使用下列代码。

<form>
  <label>Email Address
    <input type="email" placeholder="me@example.com" required>
  </label>
  <input type="submit">
</form>

JSFiddle 链接

从HTML5 spec:

有效的电子邮件地址是一条符合下一个ABNF的电子邮件生产的行,字符设置为 Unicode. 电子邮件 = 1*( atext / "." ) “@” 标签 *( "." 标签 ) 标签 = let-dig [ [ [ ldh-str ] let-dig ] ; 限于 RFC 1034 节 3.5 节 atext = < 如 RFC 5322 节 3.2.3 定义 > let-dig = < 如 RFC 1034 节 3.5 定义 > ldh-str

<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;
    }
    }

一个很好的做法是验证客户端的数据,但双重检查服务器上的验证。 考虑到这一点,您可以简单地检查是否链接看起来像客户端的有效电子邮件地址,并在服务器上进行严格的检查。

下面是我使用的JavaScript功能来检查一个字符串是否看起来像一个有效的邮件地址:

function looksLikeMail(str) {
    var lastAtPos = str.lastIndexOf('@');
    var lastDotPos = str.lastIndexOf('.');
    return (lastAtPos < lastDotPos && lastAtPos > 0 && str.indexOf('@@') == -1 && lastDotPos > 2 && (str.length - lastDotPos) > 2);
}

解释:

lastAtPos < lastDotPos: Last @ 应该是之前的最后. 因为 @ 不能是服务器名称的一部分(我知道的范围内)。 lastAtPos > 0: 应该是某些东西(电子邮件用户名)之前的最后 @. str.indexOf('@') == -1: 应该是没有 @@ 在地址. 即使 @ 出现在电子邮件用户名中的最后一个字符,它必须引用这样“ 会是 @ 和最后一个 @ 在地址。