Logo Questions Linux Laravel Mysql Ubuntu Git Menu

email validation javascript

is this javascript function (checkValidity) correct?

function checkTextBox(textBox)
   if (!checkValidity(textBox.getValue()))
       displayError("Error title", "Error message", textBox);

function checkValidity(e) 
    var email;
    email = "/^[^@]+@[^@]+.[a-z]{2,}$/i";

    if (!e.match(email)){
            return false;
            return true;

EDIT: All the answers appreciated! Thanks!

like image 778
input Avatar asked Dec 06 '22 02:12


2 Answers

E-mail address are defined in RFC 5322, § 3.4. The relevant non-terminal is addr-spec. The definition turns out to be somewhat squirelly, due to both the complications of domain specifications and supporting old, obsolete forms. However, you can do an over-approximation for most forms with:


Notice that there are a very large number of legal characters. Most reg-exs get the list wrong. Yes, all those characters are legal in an e-mail address.

This regex will not match some very uncommon used forms like "noodle soup @ 9"@[what the.example.com] -- which is a legal e-mail address!

like image 78
MtnViewMark Avatar answered Dec 07 '22 15:12


function isValidEmail($email)
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);

if(isValidEmail("[email protected]"))
  echo "valid";
   echo "aaa";
like image 35
cosy Avatar answered Dec 07 '22 14:12