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


当前回答

如果您想要使用 JQuery 并想要具有现代化的方法,则使用 JQuery 输入面具与验证。

http://bseth99.github.io/项目/jquery-ui/5-jquery-masks.html

关于如何简单的jQuery输入面具的演示文稿在这里: http://codepen.io/anon/pen/gpRyBp

一个简单的输入面具的例子为日期 forexample 没有完整的验证

 <input id="date" type="text" placeholder="YYYY-MM-DD"/>

还有剧本:

 $("#date").mask("9999-99-99",{placeholder:"YYYY-MM-DD"});

其他回答

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

如果你正在使用NG模式和材料,这就是工作。

vm.validateEmail = '([a-zA-Z0-9_.]{1,})((@[a-zA-Z]{2,})[\\\.]([a-zA-Z]{2}|[a-zA-Z]{3}))';

最好的一个 :D(RFC友好和没有错误“太复杂”) :

function    isMail(mail)
{
    pattuser = /^([A-Z0-9_%+\-!#$&'*\/=?^`{|}~]+\.?)*[A-Z0-9_%+\-!#$&'*\/=?^`{|}~]+$/i;
    pattdomain = /^([A-Z0-9-]+\.?)*[A-Z0-9-]+(\.[A-Z]{2,9})+$/i;

    tab = mail.split("@");
    if (tab.length != 2)
        return false;
    return (pattuser.test(tab[0]) && pattdomain.test(tab[1]));
}

好吧,有很多答案包含一些不同的常规表达式,我尝试了很多,我得到了不同的结果和各种不同的问题。

对于 UI 验证,我对寻找 @ 标志的最基本检查很好,重要的是要注意的是,我总是用标准的“验证电子邮件”进行服务器侧验证,其中包含用户确认他们的电子邮件地址的独特链接。

if (email.indexOf('@') > 0)

我故意选择了0甚至以零为基础,因为它也确保在 @ 之前有一个单个字符。

我正在使用这个功能

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