很久没弄javascript了,刚复习了一下,弄了个ip,email,date,time的验证函数,觉得挺实用的,故在此共享出来,希望能给大家一点帮助^_^
Code
<script language="javascript" type="text/javascript">
var patterms = new Object();
//验证IP
patterms.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}$/;
//验证EMAIL
patterms.email = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
//验证日期格式2009-07-13
patterms.date = /^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
//验证时间格式16:55:39
patterms.time = new RegExp("^([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$");
//验证函数
function verify(str,pat)
{
var thePat;
thePat = patterms[pat];
if(thePat.test(str))
{
return true;
}
else
{
return false;
}
}
//测试
alert(verify("asidycom@163.com","email")+","+verify("192.168.1.1","ip")+
","+verify("16:55:39","time")+","+verify("2009-07-13","date")+","+verify("192.168","ip"));