<div class="codetitle"> <span><a style="CURSOR: pointer" data="71566" class="copybut" id="copybut71566" onclick="doCopy('code71566')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code71566"> <br><script type="text/javascript"> <BR>//更多的验证可以继续添加到errMsg对象里 <BR>var errMsg={ <BR>required:{ <BR>msg:"This field is required.", <BR>//load参数指定是否在加载的时候验证 <BR>test:function(obj,load){ <BR>return obj.value.length > 0 || load || obj.value==obj.defaultValue; <BR>} <BR>}, <BR>//验证邮件 <BR>email:{ <BR>meg:"Not a valid email address.", <BR>test:function(obj){ <BR>return /^[a-z0-9A-Z+_.-]+\@([a-z0-9A-Z-_]+\.)+[a-z0-9]{2,4}$/i.test(obj.value); <BR>} <BR>}, <BR>//验证网址 <BR>url:{ <BR>msg:"Not a valid URL.", <BR>test:function(obj){ <BR>return obj.value=="http://" || /^https?:\/\/([a-z0-9-]+\.)+[a-z0-9]{2,4}.*$/.test(obj.value); <BR>} <BR>} <BR>} <BR>//Example <BR>onload=function(){ <BR>$("url").onblur=function(){ <BR>if(errMsg.url.test(this)){ <BR>alert("right"); <BR>}else{ <BR>alert(errMsg.url.msg); <BR>} <BR>} <BR>} <BR></script> <br> </div>