Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

is this javascript function (checkValidity) correct?

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

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

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

EDIT: All the answers appreciated! Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
207 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:

^[-0-9A-Za-z!#$%&'*+/=?^_`{|}~.]+@[-0-9A-Za-z!#$%&'*+/=?^_`{|}~.]+

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...