正文

常用的javascript验证函数2006-10-21 21:14:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/wangsdong/19591.html

分享到:

<script language="javascript">
<!--
//验证是否为空,如果为空返回 false,否则返回 true
function CheckIsNull(Inform,Inputname){
    var Form=Inform+"."
 eval("Temp="+Form+Inputname+".value;");
 if(Temp==""){
  alert("提醒您:不能为空");
  eval(Form+Inputname+".className='RedInput';");
  eval(Form+Inputname+".focus();");
  return false;
 }else{
  eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
     return true;
 }
}
//验证是否为数字
function CheckIsNum(Inform,Inputname){
    if (!CheckIsNull(Inform,Inputname))return false;
 else{
          var Form=Inform+"."
          eval("Temp="+Form+Inputname+".value;");
       if(isNaN(Temp)){
                     alert("提醒您:不为数字");
                     eval(Form+Inputname+".className='RedInput';");
                     eval(Form+Inputname+".focus();");
                     return false;
                      }
    else{
         eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
            return true;
           }
     }
}
//验证是否为E-MAIL
function CheckIsEmail(Inform,Inputname){
    if (!CheckIsNull(Inform,Inputname))return false;
    else{
       var Form=Inform+"."
          eval("Temp="+Form+Inputname+".value;");
       if(Temp.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1)
           { alert("提醒您:不为EMAIL");
             eval(Form+Inputname+".className='RedInput';");
             eval(Form+Inputname+".focus();");
             return false;
              }
    else{
          eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
             return true;
        }
       }  
}
//验证是否为HTTP地址
function CheckIsHttp(Inform,Inputname){
    if (!CheckIsNull(Inform,Inputname))return false;
    else{ 
         var Form=Inform+"."
   eval("Temp="+Form+Inputname+".value;");
   if(Temp.search(/^http:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/)==-1)
         { alert("提醒您:不为HTTP");
             eval(Form+Inputname+".className='RedInput';");
             eval(Form+Inputname+".focus();");
             return false;
              }
      else{
           eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
              return true;
         }
      }
}
//验证是否为手机号码
function CheckIsMobile(Inform,Inputname){
    if (!CheckIsNull(Inform,Inputname))return false;
    else{
   var Form=Inform+"."
   eval("Temp="+Form+Inputname+".value;");
   if(Temp.search(/^1[3|5]\d{9}$/)==-1)
   {   alert("提醒您:不为手机号码");
    eval(Form+Inputname+".className='RedInput';");
    eval(Form+Inputname+".focus();");
    return false;
   }
   else{
            eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
               return true;
        }
         }
}
//验证是否为日期
function CheckIsDate(Inform,Inputname){ 
     if(!CheckIsNull(Inform,Inputname)) return false;
     else{
         var Form=Inform+".";
         eval("Temp="+Form+Inputname+".value;");
         STemp=Temp;
         Temp="\""+Temp+"\"";
         //用空值替换分割符号以判断是否为数字,在替换时原字符串STemp不受影响                                          
         if(Temp.indexOf('-',0)!=-1){separate="-";Flag=0;TNum=STemp.replace(/-/g,"");}
         else {
               if(Temp.indexOf('/',0)!=-1){separate="/";Flag=0;TNum=STemp.replace(/\//g,"");}
        else { Flag=1;}
              }
         if(Flag==1) { alert("提醒您:没有分隔符号");
                  eval(Form+Inputname+".className='RedInput';");
                        return false;
                     }
         else { 
               if(isNaN(TNum)){alert("提醒您:日期必须为数字");
                       eval(Form+Inputname+".className='RedInput';");
        return false;
        }
               else{
                    signF=STemp.indexOf(separate);     //分隔符第一次出现的位置
                    subYY=STemp.substring(0,signF);     //年
                    signL=STemp.lastIndexOf(separate);   //分隔符最后出现的位置
                    subMM=STemp.substring(signF+1,signL);     //月
                    subDD=STemp.substring(signL+1,STemp.length); //日
                    if(subMM.length<2){subMM="0"+subMM;}           //转换月
                    if(subDD.length<2){subDD="0"+subDD;}           //转换日
                    if(subYY.length==4){subYY=subYY.substring(2,4);}
                    //Outdate=subYY+"/"+subMM+"/"+subDD;
                    var msg,waring;
                    waring="-----提醒您:发生以下错误-----"
                    msg="";
                    //月的判断
                    if(eval(subMM)>12||eval(subMM)<1){msg=waring+"\n"+"月不合法";}
                    //日的判断
                    if(msg=="")msg=waring; //说明月合法
                    else msg=msg; //月不合法时加上以上不合法信息
                    if(eval(subDD)>31||eval(subDD)<1){msg=msg+"\n"+"日不合法";}
                    if (eval(subDD)<=31){
                                          if(eval(subMM)==2){if(subDD>28)msg=waring+"\n"+"二月大于28 不合法";}
                                         }
                   if(eval(subMM)==4){if(subDD==31)msg=waring+"\n"+"四月大于30不合法";} 
                   if(eval(subMM)==6){if(subDD==31)msg=waring+"\n"+"六月大于30不合法";} 
                   if(eval(subMM)==9){if(subDD==31)msg=waring+"\n"+"九月大于30不合法";}
                   if(eval(subMM)==11){if(subDD==31)msg=waring+"\n"+"十一月大于30不合法";}
                   if(msg.length!=0){alert(msg);
                          eval(Form+Inputname+".className='RedInput';");
                          eval(Form+Inputname+".focus();");
                          return false;
           }
                  }
              }
    eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');"); 
          return ture;
        }    
}
//总验证函数
//调用参数说明:(formname,inputname) 其中formname为表单名,inputname为输入文本框名
function CheckForm(){
 if(!CheckIsHttp("myform","mytxt")) return false;
}
//-->
</script>
<style type="text/css">
.RedInput {background-color:#FF0000;}
</style>
<form id="myform" name="myform" method="post" action=""onSubmit="return CheckForm()">
  <input type="text" name="mytxt" />
  <input type="submit" name="Submit" value="提交" />
</form>

阅读(5166) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册