<!DOCTYPE html>
<html lang= "en" >
<head>
<meta charset= "UTF-8" >
<title>正则验证</title>
</head>
<body>
<input type= "text" name= "" id= "text" >
<input type= "button" name= "" id= "btn" value= "点击" >
<span></span>
</body>
<script type= "text/javascript" >
var text = document.getElementById( 'text' );
var btn = document.getElementById( 'btn' );
//1、url示例:
//https://www.baidu.com/s?wd=%E5%88%98%E5%BE%B7%E5%8D%8E&rsv_spt=1&rsv_iqid=0x9601edc200017402&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&oq=javascript%2520%25E9%259A%25BE%25E9%25A2%2598&inputT=1256&rsv_t=7da5GmMhTie86h8qaOiaV047P9TkJunMjrkmK%2BNLA%2FuJs3bMG%2Bj52w%2F6IxHge5MB%2B%2B%2Fw&rsv_pq=c270d50b00019cae&rsv_sug3=21&rsv_sug1=19&rsv_sug7=100&rsv_sug2=0&rsv_sug4=1818
//2、电话示例:
//13800138000
//+ 8610 - 59926666
//+ (1)533- 222 - 334
//010 - 59926666
//037132376865
//3、电子邮件示例:
//Handsome.W@abc.com
//_hello_world@163.com
//345@mail.some_domain_name.com.uk
// var regexp=/^([0-9a-zA-Z_.-])+@([0-9a-zA-Z_-])+(\.([a-zA-Z_-])+)+$/;//邮箱
// var regexp=/(https?.*?\.(:?cn\b|com\b|net\b|org\b|gov\b)(?!\.))/;//URL
// var regexp=/\+?\d{3,4}-?\d{7,8}/; // 010-59926666 8610-59926666 037132376865
// var regexp =/\+\(\d\)(\d{3}-){2}\d{3}/;//+(1)533-222-334
// var regexp=/(1[34578]\d{9})|(\+?\d{3,4}-?\d{7,8})|(\+\(\d\)(\d{3}-){2}\d{3})/;//电话号码 13800138000 010-59926666 8610-59926666 037132376865 +(1)533-222-334
var regexp=/(([0-9a-zA-Z_.-])+@([0-9a-zA-Z_-])+(\.([a-zA-Z_-])+)+)|((https?.*?\.(:?cn\b|com\b|net\b|org\b|gov\b)(?!\.)))|(1[34578]\d{9})|(\+?\d{3,4}-?\d{7,8})|(\+\(\d\)(\d{3}-){2}\d{3})/;
btn.onclick = function (){
if (regexp.test(text.value)){
alert( '验证通过' );
text.value= "" ;
} else {
alert( '验证没通过' );
}
}
</script>
</html>
|