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


当前回答

有一些复杂的RegEx写在这里,这也工作。

我试过这个,它也工作了:

[a-zA-Z0-9._]+[@]+[a-zA-Z0-9]+[.]+[a-zA-Z]{2,6}

请点击此处: http://www.regextester.com/?fam=97334

希望这能帮助。

其他回答

這是從 http://codesnippets.joyent.com/posts/show/1917 偷走的

email = $('email');
filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(email.value)) {
  // Yay! valid
  return true;
}
else
  {return false;}

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

如果您正在使用 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');
} 

我的常规表达的知识并不那么好,这就是为什么我首先用简单的常规表达来检查整体合成,然后检查其他功能的更具体选项,这可能不是最好的技术解决方案,但这就是为什么我更灵活、更快的方式。

我遇到的最常见的错误是空间(特别是在开始和结束),有时是双点。

function check_email(val){
    if(!val.match(/\S+@\S+\.\S+/)){ // Jaymon's / Squirtle's solution
        // Do something
        return false;
    }
    if( val.indexOf(' ')!=-1 || val.indexOf('..')!=-1){
        // Do something
        return false;
    }
    return true;
}

check_email('check@thiscom'); // Returns false
check_email('check@this..com'); // Returns false
check_email(' check@this.com'); // Returns false
check_email('check@this.com'); // Returns true
<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;
    }
    }