Email validation using jQuery

后端 未结 30 1854
被撕碎了的回忆
被撕碎了的回忆 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:20
    if($("input#email-address").getVerimailStatus() < 0) { 
    
    (incorrect code)
    
    }
    
    if($("input#email-address").getVerimailStatus() == 'error') { 
    
    (right code)
    
    }
    
    0 讨论(0)
  • 2020-11-22 17:20

    you should see this:jquery.validate.js,add it to your project

    using it like this:

    <input id='email' name='email' class='required email'/>
    
    0 讨论(0)
  • 2020-11-22 17:20

    Validate email while typing, with button state handling.

    $("#email").on("input", function(){
        var email = $("#email").val();
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        if (!filter.test(email)) {
          $(".invalid-email:empty").append("Invalid Email Address");
          $("#submit").attr("disabled", true);
        } else {
          $("#submit").attr("disabled", false);
          $(".invalid-email").empty();
        }
      });
    
    0 讨论(0)
  • 2020-11-22 17:21

    <!-- Dont forget to include the jQuery library here -->
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    
    $(document).ready(function() {
    
        $("#validate").keyup(function(){
    
            var email = $("#validate").val();
    
            if(email != 0)
            {
                if(isValidEmailAddress(email))
                {
                    $("#validEmail").css({
                        "background-image": "url('validYes.png')"
                    });
                } else {
                    $("#validEmail").css({
                        "background-image": "url('validNo.png')"
                    });
                }
            } else {
                $("#validEmail").css({
                    "background-image": "none"
                });         
            }
    
        });
    
    });
    
    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }
    
    </script>
    
    <style>
        #validEmail
        {
            margin-top: 4px;
            margin-left: 9px;
            position: absolute;
            width: 16px;
            height: 16px;
        }
    
        .text
        {
            font-family: Arial, Tahoma, Helvetica;
        }
    </style>
    
        <title>Live Email Validation with jQuery Demo</title>
    </head>
    <body>
        <div class="text"><h1>Reynoldsftw.com - Live Email Validation</h1><h2>Type in an email address in the box below:</h2></div>
        <div><input type="text" id="validate" width="30"><span id="validEmail"></span></div>
        <div class="text"><P>More script and css style
    

    : www.htmldrive.net


    Source:htmldrive.com

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

    Landed here.....ended up here: https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address

    ...which provided the following regex:

    /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
    

    ...which I found thanks to a note on the jQuery Validation plugin readme: https://github.com/jzaefferer/jquery-validation/blob/master/README.md#reporting-an-issue

    So, the updated version of @Fabian's answer would be:

    function IsEmail(email) {
      var regex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
      return regex.test(email);
    }
    

    Hope that helps

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

    jQuery Function to Validate Email

    I really don’t like to use plugins, especially when my form only has one field that needs to be validated. I use this function and call it whenever I need to validate an email form field.

     function validateEmail($email) {
      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
      return emailReg.test( $email );
    }
    

    and now to use this

    if( !validateEmail(emailaddress)) { /* do stuff here */ }
    

    Cheers!

    0 讨论(0)
提交回复
热议问题