phil crowe
phil crowe

Reputation: 663

regular expression check email is valid as well as more than one character

im trying to validate an email address with this regular expression .*@.*\..* im just wondering how i can change this to also check if the string is more than one character?

Upvotes: 0

Views: 641

Answers (5)

Saurabh
Saurabh

Reputation: 5727

Use below for email validation

function chkEmail(theField,msg)
{
        var a=theField.value;
        var reg_mail=(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+(\.[A-Za-z0-9]{2,3})(\.[A-Za-z0-9]{2,3})?$/);
        if((a.search(reg_mail)==-1))
          { 
            alert(msg);
            theField.focus();
            return false;            
          }
          return true;          
}

Use below for length

if(document.getElementById('id').value.length > 1)
{

}

Upvotes: 0

Guffa
Guffa

Reputation: 700152

Use the + specifier instead of * to make sure that there is at least one character:

.+@.+\..+

This will actually ensure that there is at least five characters, as you can't have a public email address that makes sense with less than that. You could make a more elaborate check (for example based on the minimum length of domain names and the allowed characters), but this at least covers the most basic requirements.

Upvotes: 3

Mr47
Mr47

Reputation: 2655

.+@.*\..* would also do the trick

Upvotes: 0

e.dan
e.dan

Reputation: 7475

Validating email addresses with a regex is non-trivial. See here and here.

Upvotes: 0

Tom Gullen
Tom Gullen

Reputation: 61729

You don't need to do length checks with Regex, just use the string length option:

string Email = "[email protected]";

// Regex checks here

if(Email.Length > 1){

}

Also I would recommend not validating email addresses. It's insanely complicated, see this question for more information:

Using a regular expression to validate an email address

Upvotes: 1

Related Questions