Email validation using jQuery

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

    Look at http: //bassistance.de/jquery-plugins/jquery-plugin-validation/. It is nice jQuery plugin, which allow to build powerfull validation system for forms. There are some usefull samples here. So, email field validation in form will look so:

    $("#myform").validate({
      rules: {
        field: {
          required: true,
          email: true
        }
      }
    });
    

    See Email method documentation for details and samples.

    0 讨论(0)
  • 2020-11-22 17:31
    checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
    

    Refernce : JQUERY UI WEBSITE

    0 讨论(0)
  • 2020-11-22 17:35

    A simplified one I've just made, does what I need it to. Have limited it to just alphanumeric, period, underscore and @.

    <input onKeyUp="testEmailChars(this);"><span id="a"></span>
    function testEmailChars(el){
        var email = $(el).val();
        if ( /^[a-zA-Z0-9_@.-]+$/.test(email)==true ){
            $("#a").html("valid");
        } else {
            $("#a").html("not valid");
        }
    }
    

    Made with help from others

    0 讨论(0)
  • 2020-11-22 17:37

    As others have mentioned you can use a regex to check if the email address matches a pattern. But you can still have emails that match the pattern but my still bounce or be fake spam emails.

    checking with a regex

    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
    

    checking with a real email validation API

    You can use an API which will check if the email address is real and currently active.

    var emailAddress = "foo@bar.com"
    response = $.get("https://isitarealemail.com/api/email/validate?email=" +
        emailAddress,
        function responseHandler(data) {
            if (data.status === 'valid') {
                // the email is valid and the mail box is active
            } else {
                // the email is incorrect or unable to be tested.
            }
        })
    

    For more see https://isitarealemail.com or blog post

    0 讨论(0)
  • 2020-11-22 17:37

    You can create your own function

    function emailValidate(email){
        var check = "" + email;
        if((check.search('@')>=0)&&(check.search(/\./)>=0))
            if(check.search('@')<check.split('@')[1].search(/\./)+check.search('@')) return true;
            else return false;
        else return false;
    }
    
    alert(emailValidate('your.email@yahoo.com'));
    
    0 讨论(0)
  • 2020-11-22 17:38

    You can use regular old javascript for that:

    function isEmail(email) {
      var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      return regex.test(email);
    }
    
    0 讨论(0)
提交回复
热议问题