简体   繁体   English

在JavaScript中验证电子邮件地址

[英]Validate email address in JavaScript

Here is my issue: I'm trying to validate an email in the textbox with asp:RegularExpressionValidator, See the below: 这是我的问题:我正在尝试使用asp:RegularExpressionValidator验证文本框中的电子邮件,请参见以下内容:

<telerik:RadTextBox ID="AlertMissedEmail"  Width="300" runat="server" Label="" MaxLength="500" TextMode="MultiLine"></telerik:RadTextBox>
                <asp:RegularExpressionValidator runat="server" ID="revEmail" ControlToValidate="AlertMissedEmail"
                                Display="None" ValidationExpression="^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)+$"
                                ValidationGroup="validationTourProperties" EnableClientScript="false" 
                                ErrorMessage="- Please enter a valid email address:(ex.) <b>john@gmail.com</b>" />

It works just fine if its one email but I want to let the user add multiple emails that can only be separated with semicolon 如果只有一封电子邮件,它就可以正常工作,但是我想让用户添加只能用分号分隔的多封电子邮件

I want first to check it up in JS so if the user inserted space or something else instead of semicolon the user will get an alert with the correct format how it should be inserted so he can fix the emails. 我想先在JS中进行检查,以便用户插入空格或其他内容(而不是分号)时,用户将收到具有正确格式的警报,该消息应如何插入,以便他可以修复电子邮件。

Please help me fix my JS Code: 请帮助我修复我的JS代码:

function formCheck(sender,args)
{
        //Getting the Email field value & text.
        var l_Email = $find("<%=AlertMissedEmail.ClientID %>").get_value();
        var l_EmailText = $find("<%=AlertMissedEmail.ClientID %>").get_textBoxValue();
        //Setting up the Reg expression for email.
        var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

        // 3). Check for reg expression of the email field.
        if (l_EmailText == "") { // text is empty email is not must.
             retValEmail = true;
        }

         if (l_EmailText != "") { // email is not empty check for reg 
            if (reg.test(l_Email)) {
                retValEmail = true;
            }
            else {
                alert("Please enter a valid email address");
                retValEmail = false;
            }
        }
}

I'm assuming that most of the code you posted works. 我假设您发布的大多数代码都能正常工作。 I think you just need to check out something like the split() function. 我认为您只需要检查类似split()函数的内容。 As soon as you get l_EmailText you could call: 一旦获得l_EmailText ,就可以调用:

var emails = l_EmailText.split(";");

to break up each of the emails and then iterate: 分解每封电子邮件,然后进行迭代:

for (var i = 0; i < emails.length; i++) {
    // individual email check code goes here
}

to check each email individually. 分别检查每个电子邮件。 Your regex expression /^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/; 您的正则表达式表达式/^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/; only appears to work for one email. 似乎只适用于一封电子邮件。

I modified you function a bit. 我修改了您的功能。 This is not tested. 这未经测试。 Hope it helps... 希望能帮助到你...

function formCheck(sender,args)
    {
            //Getting the Email field value & text.
            var l_Email = $find("<%=AlertMissedEmail.ClientID %>").get_value();
            var l_EmailText = $find("<%=AlertMissedEmail.ClientID %>").get_textBoxValue();
           //Setting up the Reg expression for email.
            var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
           var hasInvalidMail = false;

           if (l_EmailText.length < 7) { // valid email to have 7 chars at least
                 return false;
            }

            //split emails if there is ;
            var emails = [];
            if(l_EmailText.indexOf(';') >= 0)
            {
                emails = l_EmailText.split(';');
            }
            else
            {
                emails[0] = l_EmailText;
            }

            //loop all emails
            for(var i=0; i<emails.length;i++)
            {
                if (!reg.test(l_Email)) {
                    //stop checking if we find error
                    hasInvalidMail = true;
                    break;
                }
            }  
            return hasInvalidMail;            
    }

You can make some enhancement to return only the valid emails 您可以进行一些改进以仅返回有效的电子邮件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM