Nistor Alexandru
Nistor Alexandru

Reputation: 5393

Email validation not working

Hi I am trying to validate an email using regular expresions.This is my code:

       <input type="text" name="email" id="email"/>

       var email = $("input#email"),
       re =  /^[A-Za-z0-9][a-zA-Z0-9._-][A-Za-z0-9]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/; 

       if(re.test(email.val())){
           alert("email valid");
       }else{
           alert("email not valid");
       } 

No matter what I enter in the text field it says the email is not valid.What is the problem?

Upvotes: 0

Views: 447

Answers (4)

Robin Orheden
Robin Orheden

Reputation: 2764

I just answered a similar question: https://stackoverflow.com/a/12674524/340736

My recommendation to you would be, don't try to roll you're own. You'll probably do it wrong. Try to follow standards, such as RFC 822. It's not that easy to do by your self, but there are libraries that does this.

An example is Verimail.js. It follows RFC 822 but also does TLD validation according to the full list of TLDs registerd by IANA.

It's also simple to use:

var email = "[email protected]";
var verimail = new Comfirm.AlphaMail.Verimail();

verimail.verify(email, function(status, message, suggestion){
    if(status < 0){
        // Incorrect syntax!
    }else{
        // Syntax looks great!
    }
});

The above example will hit the line 'Incorrect syntax!' because of the invalid TLD 'cmo'. Besides this, it will also give a suggestion that you can return to your user, in this case, the suggestion variable will contain '[email protected]' since 'fabeook.cmo' looks a lot like 'facebook.com'.

Hope this helps!

Upvotes: 2

Fabio Poloni
Fabio Poloni

Reputation: 8371

You're using a really weird regex.

Have a look at this:

[A-Za-z0-9][a-zA-Z0-9._-][A-Za-z0-9]

This will match 1 alphanumeric letter, then a alphanumeric letter or a ./_/- and then 1 or more alphanumeric letter(s).

Try this regex from www.regular-expressions.info, which I edited a little bit.

/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9]{2,4}$/

Upvotes: 1

Tats_innit
Tats_innit

Reputation: 34117

Try this Working demo = http://jsfiddle.net/buezz/

Hope this helps your cause :) feel free to play around.

Good link: http://jquerybyexample.blogspot.com/2011/04/validate-email-address-using-jquery.html

code

function validateEmail(sEmail) {
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (filter.test(sEmail)) {
        return true;
    }
    else {
        return false;
    }
}

$(document).ready(function() {
   $('#btnValidate').click(function() {
        var sEmail = $('#txtEmail').val();
        if ($.trim(sEmail).length == 0) {
            alert('Please enter valid email address');
            e.preventDefault();
        }
        if (validateEmail(sEmail)) {
            alert('Email is valid');
        }
        else {
            alert('Invalid Email Address');
            e.preventDefault();
        }
    });
});
​

Upvotes: 2

ratbum
ratbum

Reputation: 1023

I use

function is_email(a){return /^([\w!.%+\-])+@([\w\-])+(?:\.[\w\-]+)+$/.test(a);}

for my email validation; as I recall it was from facebook's signup page a while ago.

You'd just need to do:

if (!is_email($('#email').val()) {
   alert('invalid email and all that');
}

Upvotes: 1

Related Questions