简体   繁体   中英

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:

<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.

Please help me fix my JS Code:

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. As soon as you get l_EmailText you could call:

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+)*$/; 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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