Validate email with jQuery

后端 未结 3 2062
长情又很酷
长情又很酷 2020-12-12 01:34

I have input name=\"email\" and a button on the page.

How do I validate this input in live for a valid email address? And add some class fo

相关标签:
3条回答
  • 2020-12-12 02:03

    i used this for mail validation javascript mail code

        function ValidateEmail(inputText)
        {
        var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        if(inputText.value.match(mailformat))
        {
        document.form1.text1.focus();
        return true;
        }
        else
        {
        alert("You have entered an invalid email address!");
        document.form1.text1.focus();
        return false;
        }
      }
    
    0 讨论(0)
  • 2020-12-12 02:08
    $('#email').bind('keyup', function(){
        if(this.value.test([REGULAR_EXPRESSION])
            //doStuff -- add active class
        else
            //doOtherStuff -- add inactive class
    });
    

    replace [REGULAR_EXPRESSION] with one of the regular expressions found here: http://www.regular-expressions.info/email.html

    0 讨论(0)
  • 2020-12-12 02:11

    I usually use this javascript function to validate in frontend:

    function validateEmail(email) 
    { 
     var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
     return email.match(re) 
    }
    

    it returns true or false. But anyway, you shouldn't usually only validate sensitive data in the frontend, but also on the server side.

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