身份证验证
function vsCard ( idCard) {
var regIdCard= / ^ ( ^ [ 1 - 9 ] \d { 7 } ( ( 0 \d ) | ( 1 [ 0 - 2 ] ) ) ( ( [ 0 | 1 | 2 ] \d ) | 3 [ 0 - 1 ] ) \d { 3 } $) | ( ^ [ 1 - 9 ] \d { 5 } [ 1 - 9 ] \d { 3 } ( ( 0 \d ) | ( 1 [ 0 - 2 ] ) ) ( ( [ 0 | 1 | 2 ] \d ) | 3 [ 0 - 1 ] ) ( ( \d { 4 } ) | \d { 3 } [ Xx] ) $) $/ ;
if ( regIdCard. test ( idCard) ) {
if ( idCard. length== 18 ) {
var idCardWi= new Array ( 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 ) ;
var idCardY= new Array ( 1 , 0 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ) ;
var idCardWiSum= 0 ;
for ( var i= 0 ; i< 17 ; i++ ) {
idCardWiSum+ = idCard. substring ( i, i+ 1 ) * idCardWi[ i] ;
}
var idCardMod= idCardWiSum% 11 ;
var idCardLast= idCard. substring ( 17 ) ;
if ( idCardMod== 2 ) {
if ( idCardLast== "X" || idCardLast== "x" ) {
alert ( "恭喜通过验证啦!" ) ;
} else {
alert ( "身份证号码错误!" ) ;
}
} else {
if ( idCardLast== idCardY[ idCardMod] ) {
alert ( "恭喜通过验证啦!" ) ;
} else {
alert ( "身份证号码错误!" ) ;
}
}
}
} else {
alert ( "身份证格式不正确!" ) ;
}
}
手机号码验证
方式一
function checkPhone ( ) {
var phone = document. getElementById ( 'phone' ) . value;
if ( ! ( / ^ 1 [ 34578 ] \d { 9 } $/ . test ( phone) ) ) {
alert ( "手机号码有误,请重填" ) ;
return false ;
}
}
方式二
function checkPhone ( ) {
var phone = document. getElementById ( 'phone' ) . value;
if ( ! ( / ^ 1 ( 3 | 4 | 5 | 7 | 8 ) \d { 9 } $/ . test ( phone) ) ) {
alert ( "手机号码有误,请重填" ) ;
return false ;
}
}
方式三
function checkPhone ( ) {
var phone = document. getElementById ( 'phone' ) . value;
if ( ! ( / ^ 1 ( 3 , 4 , 5 , 7 , 8 ) \d { 9 } $/ . test ( phone) ) ) {
alert ( "手机号码有误,请重填" ) ;
return false ;
}
}
固定电话号码验证
function checkTel ( ) {
var tel = document. getElementById ( 'tel' ) . value;
if ( ! / ^ ( \( \d { 3 , 4 } \) | \d { 3 , 4 } - | \s ) ? \d { 7 , 14 } $/ . test ( tel) ) {
alert ( '固定电话有误,请重填' ) ;
return false ;
}
}
邮箱验证
function checkEmail ( ) {
var email= document. getElementById ( 'email' ) . value;
var reg = / ^ ( [ a- zA- Z] | [ 0 - 9 ] ) ( \w | \- ) + @[ a- zA- Z0 - 9 ] + \. ( [ a- zA- Z] { 2 , 4 } ) $/ ;
if ( reg. test ( email) ) {
alert ( "邮箱格式正确" ) ;
} else {
alert ( "邮箱格式不正确" ) ;
}
}
网页地址验证
function checkUrl ( ) {
var url= document. getElementById ( 'url' ) . value;
var reg = / ^ ( https? : \/ \/ ) ? ( [ \da - z\. - ] + ) \. ( [ a- z\. ] { 2 , 6 } ) ( [ \/ \w \. - ] * ) * \/ ? $/ ;
if ( reg. test ( url) ) {
alert ( "网页地址格式正确" ) ;
} else {
alert ( "网页地址格式不正确" ) ;
}
}
IP地址验证
function checkIp ( ) {
var ip= document. getElementById ( 'ip' ) . value;
var reg = / ^ ( ? : ( ? : 25 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 01 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ) { 3 } ( ? : 25 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 01 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) $/ ;
if ( reg. test ( ip) ) {
alert ( "IP地址格式正确" ) ;
} else {
alert ( "IP地址格式不正确" ) ;
}
}
中文字符验证
function checkCode ( ) {
var code= document. getElementById ( 'code' ) . value;
var reg = [ \u4e00 - \u9fa5 ] ;
if ( reg. test ( code) ) {
alert ( "中文字符格式正确" ) ;
} else {
alert ( "中文字符格式不正确" ) ;
}
}
QQ号验证
function checkQq ( ) {
var qq= document. getElementById ( 'qq' ) . value;
var reg = [ 1 - 9 ] [ 0 - 9 ] { 4 , } ;
if ( reg. test ( qq) ) {
alert ( "qq号格式正确" ) ;
} else {
alert ( "qq号格式不正确" ) ;
}
}