Email validation using jQuery

后端 未结 30 1863
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 16:52

I\'m new to jQuery and was wondering how to use it to validate email addresses.

30条回答
  •  感情败类
    2020-11-22 17:27

    This question is more dificult to answer than seems at first sight. If you want to deal with emails correctly.

    There were loads of people around the world looking for "the regex to rule them all" but the truth is that there are tones of email providers.

    What's the problem? Well, "a_z%@gmail.com cannot exists but it may exists an address like that through another provider "a__z@provider.com.

    Why? According to the RFC: https://en.wikipedia.org/wiki/Email_address#RFC_specification.

    I'll take an excerpt to facilitate the lecture:

    The local-part of the email address may use any of these ASCII characters:
    
    - uppercase and lowercase Latin letters A to Z and a to z;
    - digits 0 to 9;
    - special characters !#$%&'*+-/=?^_`{|}~;
    - dot ., provided that it is not the first or last character unless quoted, and provided also that it does not appear consecutively unless quoted (e.g. John..Doe@example.com is not allowed but "John..Doe"@example.com is allowed);[6]
    Note that some mail servers wildcard local parts, typically the characters following a plus and less often the characters following a minus, so fred+bah@domain and fred+foo@domain might end up in the same inbox as fred+@domain or even as fred@domain. This can be useful for tagging emails for sorting, see below, and for spam control. Braces { and } are also used in that fashion, although less often.
    - space and "(),:;<>@[\] characters are allowed with restrictions (they are only allowed inside a quoted string, as described in the paragraph below, and in addition, a backslash or double-quote must be preceded by a backslash);
    - comments are allowed with parentheses at either end of the local-part; e.g. john.smith(comment)@example.com and (comment)john.smith@example.com are both equivalent to john.smith@example.com.
    

    So, i can own an email address like that:

    A__z/J0hn.sm{it!}h_comment@example.com.co
    

    If you try this address i bet it will fail in all or the major part of regex posted all across the net. But remember this address follows the RFC rules so it's fair valid.

    Imagine my frustration at not being able to register anywhere checked with those regex!!

    The only one who really can validate an email address is the provider of the email address.

    How to deal with, so?

    It doesn't matter if a user adds a non-valid e-mail in almost all cases. You can rely on HTML 5 input type="email" that is running near to RFC, little chance to fail. HTML5 input type="email" info: https://www.w3.org/TR/2012/WD-html-markup-20121011/input.email.html

    For example, this is an RFC valid email:

    "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com
    

    But the html5 validation will tell you that the text before @ must not contain " or () chars for example, which is actually incorrect.

    Anyway, you should do this by accepting the email address and sending an email message to that email address, with a code/link the user must visit to confirm validity.

    A good practice while doing this is the "enter your e-mail again" input to avoid user typing errors. If this is not enough for you, add a pre-submit modal-window with a title "is this your current e-mail?", then the mail entered by the user inside an h2 tag, you know, to show clearly which e-mail they entered, then a "yes, submit" button.

提交回复
热议问题