如何使用JQuery来验证电子邮件地址?


当前回答

这个regexp可以防止重复的域名,比如abc@abc.com.com.com.com,它只允许域名两次,比如abc@abc.co.in。它也不允许从123abc@abc.com这样的数字启动

 regexp: /^([a-zA-Z])+([a-zA-Z0-9_.+-])+\@(([a-zA-Z])+\.+?(com|co|in|org|net|edu|info|gov|vekomy))\.?(com|co|in|org|net|edu|info|gov)?$/, 

万事如意!!!!!

其他回答

Jquery验证插件只验证@来改变这一点

将代码更改为这样

email: function( value, element ) {
    // From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
    // Retrieved 2014-01-14
    // If you have a problem with this implementation, report a bug against the above spec
    // Or use custom methods to implement your own email validation
    return this.optional( element ) || /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test( value );
}

我刚刚做了一个简化的,可以满足我的需要。 把它限制为字母数字,句号,下划线和@。

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

在别人的帮助下完成

使用这个

if ($this.hasClass('tb-email')) {
    var email = $this.val();
    var txt = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!txt.test(email)) {
        e.preventDefault();
        $this.addClass('error');
    } else {
        $this.removeClass('error');
    }
}

这个regexp可以防止重复的域名,比如abc@abc.com.com.com.com,它只允许域名两次,比如abc@abc.co.in。它也不允许从123abc@abc.com这样的数字启动

 regexp: /^([a-zA-Z])+([a-zA-Z0-9_.+-])+\@(([a-zA-Z])+\.+?(com|co|in|org|net|edu|info|gov|vekomy))\.?(com|co|in|org|net|edu|info|gov)?$/, 

万事如意!!!!!

jQuery函数验证电子邮件

我真的不喜欢使用插件,特别是当我的表单只有一个需要验证的字段时。我使用这个函数,并在需要验证电子邮件表单字段时调用它。

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

现在用这个

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