<div class="codetitle"> <span><a style="CURSOR: pointer" data="69286" class="copybut" id="copybut69286" onclick="doCopy('code69286')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code69286"> <br> <br> <br> <br><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <br><title>表单验证</title> <br><script language="javascript"> <BR>function check_1(param){//不为空 <BR>if(param==""||param==null){return false;}else{return true;} <BR>} <BR>function check_2(param){//长度限制,字母是10个,汉字也是10个 <BR>if(param.length>10){return false;}else{return true;} <BR>} <BR>function check_3(param){//只能输入汉字 <BR>var pattern= /^[\u4e00-\u9faf]+$/; <BR>var flag = pattern.test(param); <BR>if(flag==false){return false;}else{return true;} <BR>} <BR>function check_4(param){//只能输入数字 <BR>var pattern= /^[0-9]+$/; <BR>var flag = pattern.test(param); <BR>if(flag==false){return false;}else{return true;} <BR>} <BR>function check_5(param){//只能输入数字字母下划线 <BR>var pattern= /^[0-9a-zA-z_]+$/; <BR>var flag = pattern.test(param); <BR>if(flag==false){return false;}else{return true;} <BR>} <BR>function check_6(param){//邮箱格式验证 <BR>var pattern= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; <BR>var flag = pattern.test(param); <BR>if(flag==false){return false;}else{return true;} <BR>} <BR>function check_7(param){//网址格式验证 <BR>var pattern= /^((https|http|ftp|rtsp|mms)?:\/\/)?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)$/; <BR>var flag = pattern.test(param); <BR>if(flag==false){return false;}else{return true;} <BR>} <BR>function check(){ <BR>var value=document.getElementById("testValue").value; <BR>var result=check_7(value);//在这里设置验证函数的名称,我写了check_1~check_7七个常用的表单验证函数 <BR>if(result==false){ <BR>document.getElementById("testValue").style.border="2px solid red"; <BR>}else{ <BR>//document.getElementById("testValue").style.border="2px solid green"; <BR>document.getElementById("testValue").style.border=""; <BR>} <BR>} <BR></script> <br> <br> <br><div style="text-align: center;padding: 200px 0;"> <br>请输入: <input id="testValue" name="" class="" type="text" style="width:200px;height:30px;"> <br><input id="" name="" class="" type="button" value="点击验证" style="width:70px;height:30px;" onclick="check()"> <br> </div> <br> <br> <br> </div>