js正则验证邮箱和手机号码格式

验证手机号码格式:

function phone(){
var a= / ^ 0 {0,1} ( 13 [ 0-9 ] | 14 [ 0-9 ] | 15 [ 0-9 ] | 17 [ 0-9 ] | 18 [ 0-9 ])[ 0-9 ] {8} $ /;
if ( a . test ( document.getElementById("phone").value )){
             //a.test()里面为手机号码输入框的的value,如果可以为空提交可以这样
             //写 if ( a . test ( document.getElementById("phone").value) ||document.getElementById("phone").value=='')                        
return true;
} else{
alert( "请输入正确的手机号码")
return false;
}
}


验证邮箱格式:

function email(){
var a= / ^ ([ a-zA-Z0-9 ] + [ _| \_ | \. ] ? ) * [ a-zA-Z0-9 ] + @ ([ a-zA-Z0-9 ] + [ _| \_ | \. ] ? ) * [ a-zA-Z0-9 ] +\. [ a-zA-Z ] {2,3} $ /;
if( a. test( $( ".email"). val())){ //a.test()里面为邮箱输入框的的value
return true;
} else{
alert( "请输入正确的邮箱")
return false;
}
}


提交表单

$( ".submit"). click( function(){    
if(( phone()&& email())== true){
                

               //这里是业务代码,当邮箱和手机号码都验证为正确的时候才能执行

          }
})
 


猜你喜欢

转载自blog.csdn.net/qq_33168578/article/details/79743245