Email validation using jQuery

后端 未结 30 2198
被撕碎了的回忆
被撕碎了的回忆 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:30

    I would recommend Verimail.js, it also has a JQuery plugin.

    Why? Verimail supports the following:

    • Syntax validation (according to RFC 822)
    • IANA TLD validation
    • Spelling suggestion for the most common TLDs and email domains
    • Deny temporary email account domains such as mailinator.com

    So besides validation, Verimail.js also gives you suggestions. So if you type an email with the wrong TLD or domain that is very similar to a common email domain (hotmail.com, gmail.com, etc), it can detect this and suggest a correction.

    Examples:

    • test@gnail.con -> Did you mean test@gmail.com?
    • test@hey.nwt -> Did you mean test@hey.net?
    • test@hottmail.com -> Did you mean test@hotmail.com?

    And so on..

    To use it with jQuery, just include verimail.jquery.js on your site and run the function below:

    $("input#email-address").verimail({
        messageElement: "p#status-message"
    });
    

    The message element is an element in which a message will be shown. This can be everything from "Your email is invalid" to "Did you mean ...?".

    If you have a form and want to restrict it so that it cannot be submitted unless the email is valid, then you can check the status using the getVerimailStatus-function as shown below:

    if($("input#email-address").getVerimailStatus() < 0){
        // Invalid
    }else{
        // Valid
    }
    

    This function returns an integer status code according to the object Comfirm.AlphaMail.Verimail.Status. But the general rule of thumb is that any codes below 0 is codes indicating errors.

提交回复
热议问题