js常用验证

validate.js

    //验证是否为空  
    function CheckIsNull(Inform,Inputname,Inputvalue){  
       var Form=Inform+"."  
    eval("Temp="+Form+Inputname+".value;");  
    if(Temp==""){  
     alert("提醒您:"+Inputvalue+"不能为空!");  
     eval(Form+Inputname+".className='RedInput';");  
     eval(Form+Inputname+".focus();");  
     return false;  
    }else{  
     eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");  
        return true;  
    }  
    }  
    //验证长度的函数,首先调用验证是否为空的函数!  
    function CheckLength(Inform,Inputname,Inputvalue,InputMinSize,InputMaxSize){  
    //验证是否为空!  
    if (!CheckIsNull(Inform,Inputname,Inputvalue)) return false;  
    else{  
    var Form=Inform+"."  
    eval("Temp="+Form+Inputname+".value;");  
    if (Temp.length<parseInt(InputMinSize)||Temp.length>parseInt(InputMaxSize)){  
    alert(Inputvalue+"的长度必须在"+InputMinSize+"-"+InputMaxSize+"之间!");  
    eval(Form+Inputname+".className='RedInput';");  
     eval(Form+Inputname+".focus();");  
    return false;}  
    else{  
     eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");  
        return true;  
    }  
    }  
    }  
    //验证长度函数二,该函数主要判断当不要求是否为空、只要求最大长度的时候使用  
    function CheckLengthTwo(Inform,Inputname,Inputvalue,InputMaxSize){  
    var Form=Inform+"."  
    eval("Temp="+Form+Inputname+".value;");  
    if (Temp.length>parseInt(InputMaxSize)){  
    alert(Inputvalue+"的长度必须小于"+InputMaxSize+"!");  
    eval(Form+Inputname+".className='RedInput';");  
     eval(Form+Inputname+".focus();");  
    return false;}  
    else{  
     eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");  
        return true;  
    }  
    }  
    //验证是否为合法email的函数  
    function CheckIsEmail(Inform,Inputname,Email){  
       if (!CheckIsNull(Inform,Inputname,Email))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;  
           }  
          }    
    }  
    //验证是否是数字  
    function CheckIsNum(Inform,Inputname,Num){  
       if (!CheckIsNull(Inform,Inputname,Num))return false;  
    else{  
             var Form=Inform+"."  
             eval("Temp="+Form+Inputname+".value;");  
          if(isNaN(Temp)){  
                        alert("提醒您:"+Num+"应该为数字!");  
                        eval(Form+Inputname+".className='RedInput';");  
                        eval(Form+Inputname+".focus();");  
                        return false;  
                         }  
       else{  
            eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");  
               return true;  
              }  
        }  
    }  
    //验证是否为电话号码  
    function CheckIsPhone(Inform,Inputname,Phone){  
       if (!CheckIsNull(Inform,Inputname,Phone))return false;  
       else{  
      var Form=Inform+"."  
      eval("Temp="+Form+Inputname+".value;");  
      if(Temp.search(/^(0[0-9]{2,3}/-)?([2-9][0-9]{6,7})+(/-[0-9]{1,4})?$/)==-1)  
      {   alert("提醒您:"+Phone+"不为合法电话号码!");  
       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,Mobile){  
       if (!CheckIsNull(Inform,Inputname,Mobile))return false;  
       else{  
      var Form=Inform+"."  
      eval("Temp="+Form+Inputname+".value;");  
      if(Temp.search(/^((/(/d{3}/))|(/d{3}/-))?13[0-9]/d{8}|15[89]/d{8}/)==-1) //新添加了158,159两个号段  
      {   alert("提醒您:"+Mobile+"不为合法手机号码!");  
       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,date){  
    //.先判断是否为空,如果为空,直接返回false  
     if(!CheckIsNull(Inform,Inputname,date)) return false;  
     else{  
    var Form=Inform+".";  
     //.再判断是否为日期格式  
     eval("DateValue="+Form+Inputname+".value;");  
     //.如果是数字的话,返回false  
     if(DateValue.substring(4,5)!="-"||isNaN(DateValue.substring(0,4))||DateValue.substring(0,1)=="0"){  
     alert(date+"格式不正确,格式:yyyy-mm-dd,且年份第一位不能为0,不能包含字母!");  
     eval(Form+Inputname+".className='RedInput';");  
     eval(Form+Inputname+".focus();");  
     return false;  
     }else{//.总体判断月开始  
      if(DateValue.indexOf("-")==DateValue.lastIndexOf("-")||isNaN(DateValue.substring(5,DateValue.lastIndexOf("-")))||DateValue.substring(5,DateValue.lastIndexOf("-")).length>2||DateValue.substring(5,DateValue.lastIndexOf("-"))>12){  
      alert(date+"格式不正确,格式:yyyy-mm-dd,且月份不能大于12,不能包含字母!");  
      eval(Form+Inputname+".className='RedInput';");  
      eval(Form+Inputname+".focus();");  
      return false;  
      }else{//.总体判断日期开始  
       if(DateValue.substring(DateValue.lastIndexOf("-")+1).length>2||DateValue.substring(DateValue.lastIndexOf("-")+1)>31||isNaN(DateValue.substring(DateValue.lastIndexOf("-")+1)) ){  
       alert(date+"格式不正确,格式:yyyy-mm-dd,且日期不能大于31,不能包含字母!");  
       eval(Form+Inputname+".className='RedInput';");  
       eval(Form+Inputname+".focus();");  
       return false;  
       }else{//.开始判断月份的大小  
       var year=DateValue.substring(0,4);  
       var month=DateValue.substring(5,DateValue.lastIndexOf("-"));  
       var day=DateValue.substring(DateValue.lastIndexOf("-")+1);  
         if(parseInt(month)==2){//..判断二月的情况  
      if (year%4==0 && year%100 !=0 ||year%400 ==0 ){  
        if(day>29){  
         alert(date+"不正确,"+year+"年的二月最多只有29天!");  
            eval(Form+Inputname+".className='RedInput';");  
            eval(Form+Inputname+".focus();");  
            return false;  
        }  
      }else{  
       if(day>28){  
         alert(date+"不正确,"+year+"年的二月最多只有28天!");  
            eval(Form+Inputname+".className='RedInput';");  
            eval(Form+Inputname+".focus();");  
            return false;  
        }  
      }  
      }//.判断二月结束  
      else if(parseInt(month)==1||parseInt(month)==3||parseInt(month)==7||parseInt(month)==8||month==10||month==12){//.判断有31天月份开始  
         if(day>31){  
      alert(date+"不正确,"+parseInt(month)+"月最多只有31天!");  
            eval(Form+Inputname+".className='RedInput';");  
            eval(Form+Inputname+".focus();");  
            return false;  
      }  
     }//.判断有31的函数结束。  
      else{//.有30天的情况  
         if(day>30){  
      alert(date+"不正确,"+parseInt(month)+"月最多只有30天!");  
            eval(Form+Inputname+".className='RedInput';");  
            eval(Form+Inputname+".focus();");  
            return false;  
      }  
       } //.判断日期为30天结束     
       }  //.判断月份结束  
      }//.总体判断日期结束  
     }//..总体判断月结束  
    return true;  
    }  
    }  
    //.汉字的判断  
    //.输入参数  表单名  表单内文本框的名字,文本框的含义  
    function checkChineseChar(Inputform,Inputname,Inputvalue){  
    if (!CheckIsNull(Inputform,Inputname,Inputvalue))return false;  
    var Form=Inputform+".";  
    var Name=Inputname;  
    eval("Value="+Form+Name+".value;");  
    eval("Len="+Form+Name+".value.length;");  
    for(i=0;i<Len;i++){  
    char=Value.charCodeAt(i);  
    if(!(char>255)){  
        alert("提醒您:"+Inputvalue+"只能是汉字!");  
        eval(Form+Inputname+".className='RedInput';");  
        eval(Form+Inputname+".focus();");  
    return false;  
    }  
    }  
    return true;  
    }  
    //验证是否为IP  
    function CheckIsIP(Inform,Inputname,IP){  
       if (!CheckIsNull(Inform,Inputname,IP))return false;  
       else{  
      var Form=Inform+"."  
      eval("Temp="+Form+Inputname+".value;");  
      if(Temp.search(/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/)==-1)  
      {   alert("提醒您:"+IP+"不为合法IP地址!");  
       eval(Form+Inputname+".className='RedInput';");  
       eval(Form+Inputname+".focus();");  
       return false;  
      }  
      else{  
               eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");  
                  return true;  
           }  
     }  
    }  
    //验证是否为URL  
    function CheckIsURL(Inform,Inputname,URL){  
       if (!CheckIsNull(Inform,Inputname,URL))return false;  
       else{  
      var Form=Inform+"."  
      eval("Temp="+Form+Inputname+".value;");  
      if(Temp.search(/^http/://///w+(/./w+)*(///w+)*(/?/w+=/w*(&/w+=/w*)*)?$/g)==-1)  
      {   alert("提醒您:"+URL+"不为合法URL地址!");  
       eval(Form+Inputname+".className='RedInput';");  
       eval(Form+Inputname+".focus();");  
       return false;  
      }  
      else{  
               eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");  
                  return true;  
           }  
     }  
    }  
validate.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="validate.aspx.cs" Inherits="validate" %>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    <html xmlns="http://www.w3.org/1999/xhtml" >  
    <head id="Head1" runat="server">  
        <title>无标题页</title>  
        <mce:script type="text/javascript" src="js/validate.js" mce_src="js/validate.js"></mce:script>  
       <mce:script type="text/javascript"><!--  
    function CheckForm(){  
    //调用是否为空的函数  
    if(!CheckIsNull("form1","txtUserName","用户名")) return false;  
    //调用长度验证的函数  
    if(!CheckLength("form1","txtUserName","用户名","5","10")) return false;  
    //调用是否为空的函数  
    //if(!CheckIsNull("form1","txtPassword","密码")) return false;  
    //调用只验证最大长度的函数  
    if(!CheckLengthTwo("form1","txtPassword","密码","10")) return false;  
    //调用是否为合法email的函数  
    if (!CheckIsEmail("form1","txtEmail","电子邮件地址")) return false;  
    //调用验证是否是数字的函数  
    if (!CheckIsNum("form1","txtNumber","数字")) return false;  
    //调用是否为合法电话号码的函数  
    if (!CheckIsPhone("form1","txtPhone","格式应如xxx-xxxxxxxx,xxxx-xxxxxxx。电话号码")) return false;  
    //调用是否为合法手机号码的函数  
    if (!CheckIsMobile("form1","txtMobile","手机号码")) return false;  
    //验证文本框中输入的是否为日期  
    if (!CheckIsDate("form1","txtDate","日期")) return false;  
    //汉字判断测试  
    if(!checkChineseChar("form1","txtChinesechar","汉字判断测试")) return false;  
    //IP判断测试  
    if(!CheckIsIP("form1","txtIP","IP判断测试")) return false;  
    //网址判断测试  
    if(!CheckIsURL("form1","txtUrl","网址判断测试")) return false;  
    }  
    // --></mce:script>  
    </head>  
    <body>  
    <form runat="server" id="form1">  
        用户名: <asp:TextBox runat="server" ID="txtUserName" Text="">ddfdss</asp:TextBox><br />  
        密码: <asp:TextBox runat="server" ID="txtPassword" Text="">dfdsf</asp:TextBox><br />  
        email: <asp:TextBox runat="server" ID="txtEmail" Text="">[email protected]</asp:TextBox><br />  
        数字: <asp:TextBox runat="server" ID="txtNumber" Text="">123</asp:TextBox><br />  
        电话号码: <asp:TextBox runat="server" ID="txtPhone" Text="">13012345678</asp:TextBox><br />  
        手机号码: <asp:TextBox runat="server" ID="txtMobile" Text="">13012345678</asp:TextBox><br />  
        日期:<asp:TextBox runat="server" ID="txtDate" Text="">2009-3-31</asp:TextBox><br />  
        汉字:<asp:TextBox runat="server" ID="txtChinesechar" Text="">汉字</asp:TextBox><br />  
        IP:<asp:TextBox runat="server" ID="txtIP" Text="">10.1.5.50</asp:TextBox><br />        
        网址:<asp:TextBox runat="server" ID="txtUrl" Text=""></asp:TextBox><br />  
         <asp:Button runat="server" ID="btnOK" Text="提交" OnClientClick="return CheckForm()" />  
        </form>  
    </body>  
    </html>  

你可能感兴趣的:(Date,server,function,asp,email,textbox)