<script language="javascript">
var patterns=new Object();
//IP地址(***.***.***.***)
patterns.ip=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/;
//邮件(***@***.**)
patterns.email=/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
//日期(****-**-**)
patterns.date=/^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
//邮政编码
patterns.postcode=/^[1-9]{1}(\d){5}$/;
//电话号码
patterns.phone=/^[+]{0,1}\d{3,5}-?\d{5,9}$/;
//手机号码
patterns.mobilephone=/^1[356]\d{9}|01[356]\d{9}$/;
//时间(**:**:**)
patterns.time=new RegExp("^([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$");
//数字
patterns.num=/^[0-9]\d*(\.[0-9]\d*)?$/;
//------------------------------------------------------//
function verify(str,pat)
{
thePat=patterns[pat];
if(thePat.test(str))
{
return true;
}
else{
return false;
}
}
//简单的使用方法:
alert(verify("2010-3-3","date"));
alert(verify("0.32e","num"));
</script>
JS正则表达式常用工具
最新推荐文章于 2024-05-29 09:52:16 发布