简体   繁体   English

使用jQuery验证电子邮件

[英]Validate email with jQuery

I have input name="email" and a button on the page. 我在页面上input name="email"和一个button

How do I validate this input in live for a valid email address? 如何现场验证此输入是否有效的电子邮件地址? And add some class for a button, class "active" if it is valid or "inactive". 并为按钮添加一些类别,如果有效或为“无效”类别,则为“有效”类别。

This is a small task, so I don't want to use a plugins. 这是一个小任务,所以我不想使用插件。

Thanks 谢谢

I usually use this javascript function to validate in frontend: 我通常使用此javascript函数在前端进行验证:

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. 它返回true或false。 But anyway, you shouldn't usually only validate sensitive data in the frontend, but also on the server side. 但是无论如何,您通常不应该只在前端验证敏感数据,还应该在服务器端验证敏感数据。

$('#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 将[REGULAR_EXPRESSION]替换为此处找到的正则表达式之一: http : //www.regular-expressions.info/email.html

i used this for mail validation javascript mail code 我将此用于邮件验证javascript邮件代码

    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;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM