关于验证:使用jQuery和regex验证电子邮件地址

Validating email addresses using jQuery and regex

我不太确定该怎么做。我需要使用regex验证电子邮件地址,如下所示:

1
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)

然后我需要在jquery函数中运行这个函数,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$j("#fld_emailaddress").live('change',function() {
var emailaddress = $j("#fld_emailaddress").val();

// validation here?

if(emailaddress){}

// end validation

$j.ajax({  
        type:"POST",  
         url:"../ff-admin/ff-register/ff-user-check.php",  
        data:"fld_emailaddress="+ emailaddress,  
        success: function(msg)
        {
            if(msg == 'OK') {
            $j("#fld_username").attr('disabled',false);
            $j("#fld_password").attr('disabled',false);
            $j("#cmd_register_submit").attr('disabled',false);
            $j("#fld_emailaddress").removeClass('object_error'); // if necessary
            $j("#fld_emailaddress").addClass("object_ok");
            $j('#email_ac').html('&nbsp;<img src="img/cool.png" align="absmiddle"> <font color="Green"> Your email '+ emailaddress+' is OK.</font>  ');
            } else {  
            $j("#fld_username").attr('disabled',true);
            $j("#fld_password").attr('disabled',true);
            $j("#cmd_register_submit").attr('disabled',true);  
            $j("#fld_emailaddress").removeClass('object_ok'); // if necessary
            $j("#fld_emailaddress").addClass("object_error");
            $j('#email_ac').html(msg);
            }
        }
     });
});

验证在哪里进行,表达式是什么?


修正

  • http:///so.lucafilosofi.com jQuery -验证电子邮件地址- / -正则表达式
  • 使用新的正则表达式
  • address for added支持标签(+符号)
1
2
3
4
5
6
7
8
function isValidEmailAddress(emailAddress) {
    var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*

)?[ \t]+)?([\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]))*(([ \t]*

)?[ \t]+)?")@(([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;
    return pattern.test(emailAddress);
}
1
if( !isValidEmailAddress( emailaddress ) ) { /* do stuff here */ }
  • 注:*保持在100%,不检查正则表达式的名称的电子邮件!


解决方案:This is MyP></

1
2
3
4
5
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
    // alert( pattern.test(emailAddress) );
    return pattern.test(emailAddress);
};

found that regexp > <走过来:http:////邮件队列mdskinner.com -正则表达式-确认-></jQuery罢工P></


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(document).ready(function() {

$('#emailid').focusout(function(){

                $('#emailid').filter(function(){
                   var emil=$('#emailid').val();
              var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            if( !emailReg.test( emil ) ) {
                alert('Please enter valid email');
                } else {
                alert('Thank you for your valid email');
                }
                })
            });
});


- This is much betterP></

1
2
3
4
    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/);
        return pattern.test(emailAddress);
    };


你会使用jQuery插件recommend for the verimail.js。P></

为什么?P></

  • IANA TLD确认
  • 确认表(根据RFC 822)
  • 拼写建议for the most common顶级域名和电子邮件域
  • 临时电子邮件帐户拒绝域mailinator.com such as

知识?P></

包括你的网站和在线使用verimail.jquery.js:the functionP></

1
2
3
$("input#email-address").verimail({
    messageElement:"p#status-message"
});

如果你想有一个表单验证和提交的电子邮件"在线,你可以用the getverimailstatus函数:P></

1
2
3
4
5
if($("input#email-address").getVerimailStatus() < 0){
    // Invalid email
}else{
    // Valid email
}


JavaScript的:P></

1
2
var pattern = new RegExp("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
var result = pattern .test(str);


is not for the允许正则表达式:P></


可为:P></

开源的http:/ / / / www.mkyong.com正则表达式- 10 - java正则表达式实例-我知道-你应该/P></


你可以使用这个函数P></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 var validateEmail = function (email) {

        var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*

)?[ \t]+)?([\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]))*(([ \t]*

)?[ \t]+)?")@(([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;


        if (pattern.test(email)) {
            return true;
        }
        else {
            return false;
        }
    };

试试这个P></

1
2
3
4
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
    return pattern.test(emailAddress);
};

我们也可以使用正则表达式(/(W - [ ] ^(+)"[ ] W(+ -)(。(W){ } 2.3美元)+)/验证电子邮件地址格式)is to correct or not。P></

1
2
3
4
5
6
7
var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
 var valid = emailRegex.test(emailAddress);
  if (!valid) {
    alert("Invalid e-mail address");
    return false;
  } else
    return true;