Javascript正则表达式,Email邮箱格式验证

function checkEmailFormat(emailAddress){
   //空字符(或null)视为正确的格式
    if(emailAddress.trim() == "" || emailAddress == null ){return true;}
    emailAddress = emailAddress.trim();
    if(emailAddress  == ""){return true;}

    
    //正确的邮箱格式:[email protected]
    //错误的邮箱格式: [email protected]
    //以a-z或A-Z或0-9开头的多个字串+"@"+ (a-z或A-Z)的字符串 + "." + (a-z或A-Z)的字符串结尾
    var patt1 = /^[a-z,A-Z,0-9]+@[a-z,A-Z]+.[a-z,A-Z]+$/
    if(str.match(patt1) == null){
        return false;
    }else{return true;}

}


调用:

if(checkEmailFormat('[email protected]')){
  alert("格式正确");  //这里将会执行到
}

if(checkEmailFormat('[email protected]')){
  alert("格式正确");  //这里不会执行到
}

if(checkEmailFormat('[email protected]')){
  alert("格式正确");  //这里也不会执行到
}
//。。。。除了第一种情况外, 函数检查全都返回false值

 

你可能感兴趣的:(Web开发,正则表达式)