使用jQuery和正则表达式验证电子邮件地址

我不太确定如何做到这一点。 我需要使用正则表达式来验证电子邮件地址,如下所示:

[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函数中运行它:

$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 <strong>'+ emailaddress+'</strong> 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-validate-e-mail-address-regex/
  • 使用新的正则表达式
  • 添加了对地址标记(+号)的支持

  • function isValidEmailAddress(emailAddress) {
        var pattern = /^([a-zd!#$%&'*+-/=?^_`{|}~u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]+(.[a-zd!#$%&'*+-/=?^_`{|}~u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]+)*|"((([ t]*rn)?[ t]+)?([x01-x08x0bx0cx0e-x1fx7fx21x23-x5bx5d-x7eu00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]|[x01-x09x0bx0cx0d-x7fu00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))*(([ t]*rn)?[ t]+)?")@(([a-zdu00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]|[a-zdu00A0-uD7FFuF900-uFDCFuFDF0-uFFEF][a-zd-._~u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]*[a-zdu00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]).)+([a-zu00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]|[a-zu00A0-uD7FFuF900-uFDCFuFDF0-uFFEF][a-zd-._~u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]*[a-zu00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]).?$/i;
        return pattern.test(emailAddress);
    }
    

    if( !isValidEmailAddress( emailaddress ) ) { /* do stuff here */ }
    
  • 注意:请记住,没有100%的正则表达式电子邮件检查存在!

  • 这是我的解决方案:

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

    在这里发现RegExp:http://mdskinner.com/code/email-regex-and-validation-jquery


    $(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');
                    }
                    })
                });
    });
    
    链接地址: http://www.djcxy.com/p/16545.html

    上一篇: Validating email addresses using jQuery and regex

    下一篇: var($email, FILTER