<script type="text/javascript">
//更多的验证可以继续添加到errMsg对象里
var errMsg={
required:{
msg:"This field is required.",
//load参数指定是否在加载的时候验证
test:function(obj,load){
return obj.value.length > 0 || load || obj.value==obj.defaultValue;
}
},
//验证邮件
email:{
meg:"Not a valid email address.",
test:function(obj){
return /^[a-z0-9A-Z+_.-]+\@([a-z0-9A-Z-_]+\.)+[a-z0-9]{2,4}$/i.test(obj.value);
}
},
//验证网址
url:{
msg:"Not a valid URL.",
test:function(obj){
return obj.value=="http://" || /^https?:\/\/([a-z0-9-]+\.)+[a-z0-9]{2,4}.*$/.test(obj.value);
}
}
}
//Example
onload=function(){
$("url").onblur=function(){
if(errMsg.url.test(this)){
alert("right");
}else{
alert(errMsg.url.msg);
}
}
}
</script>