中国教程网论坛's Archiver

wingbird 发表于 2008-6-28 21:22

Web开发通用表单检测函数库

//Wingbird_Security_Function_Storeroom
//--------------------------------------------------------------------------------
// 库    名:Web开发通用表单检测函数库
// 关于作者:飞翔鸟 QQ:343341961 [url]www.soft263.net[/url]
// 功能说明:集中的对用户输入进行检查
//--------------------------------------------------------------------------------

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//   函数名:CHKInput(obj,chknull,isnull,chktype,security,starend,strlen)                                            ::
//   功  能:表单内容检测模块                                                                                        ::
//   参  数: obj      :  为表单名称(ID)一般用this返回;                                                             ::
//           chknull  :  为0时不进行为空检测,为1时才进行为空检测,一般是在提交按钮上才设为1                          ::
//                       <form  name="form1" method="POST" action="search1.jsp"                                      ::
//                        onsubmit="return CHKInput(this,1,[0],'0',0,[0,0],0)">                                      ::
//           chktype : 为类型,参照函数内释                                                                         ::
//           isnull   : 说明控件的输入是否可以为空,如果里面的值有小于0的则不检测 ,最好标为-1                         ::                                                       ::
//                    为 1:不可以 0:可以;所有表单内的输入控件的指定从0开始                                         ::                                                ::
//           security : 为1时要进行安全检查,为0时不进行检查                                                         ::
//   Author: WingBird : QQ(343341961)                                                                                ::
// 调用方法:<input type="text" name="n1"  onBlur=" CHKInput(this,0,[0,-1,50],'isnumeric',1,[0,0],0)" accesskey="s"> ::
//                                               返回控件ID   |   |     |       |         |  |  |  |                 ::
//                                                是否进行为空检测|     |       |         |  |  |  |                 ::
//                                                    声明不能为空的控件|       |         |  |  |  |                 ::
//                                                                 匹配的数据类型         |  |  |  |                 ::
//                                                                  是否进行数据的安全检查|  |  |  |                 ::
//                                                                         验证数字时的最小数|  |  |                 ::
//                                                                           验证数字时的最大数 |  |                 ::      
//                                                                                最多可输入的字符数                 ::
//                                                                                                                   ::
//--------------------匹配的数据类型参数chktype说明:-------------------------------------------------------- -       ::
// "isletter_l"       ://判断是否由26个英文字母的L小写组成的字符串                                                   ::
// "isletter_u"       ://判断是否为由26个英文字母的U大写组成的字符串                                                 ::
// "isletter_numeric_"://判断是否由数字、26个英文字母或者下划线组成的字符串                                          ::
// "isnumeric"        ://判断是否为数字                                                                              ::
// "isdecimal"        ://匹配小数                                                                                    ::
// "isnegative"       ://要求输入负数                                                                                ::
// "isnumeric_letter" ://判断是否由数字和26个英文字母组成的字符串                                                    ::
// "isuser"           ://判断是否为用户名格式                                                                        ::
// "ischinese"        ://判断是否为汉字                                                                              ::
// "isemail"          ://判断是否为邮箱格式                                                                          ::
// "ishttp"           ://判断是否为http格式                                                                          ::
// "isdate_-"         ://判断是否为合法双位日期格式以-分开                                                           ::
// "isdate_/"         ://判断是否为合法双位日期格式以/分开                                                           ::
// "isdate_ymd"       ://判断是否为合法双位日期格式以 年 月  日的形式                                                ::
// "istel"            ://检查是否为电话号码                                                                          ::
// "ismobiletelephone"://检查是否为手机号码                                                                          ::
// "ispostalcode"     ://检查是否为邮政编号                                                                          ::
// "isip"             ://检查是否为IP地址                                                                            ::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//============屏蔽错误=============
//function killErrors()
//{return true;}
//window.onerror = killErrors;
//==================================
function CHKInput(obj,chknull,isnull,chktype,security,starend,strlen)
{
       
if( chknull!=0)
{
   return yn_null(obj,isnull)
}

if( obj.value>0&&strlen!=0)
{
    if(obj.value.length>strlen)
   {
    alert("对不起,请输入"+strlen+"个以内的字符。");
    obj.focus();
        return false;
   }
}
//=============对字符进行安全检查==========
if(security==1)
{
    var i,j,strTemp,arrstr;
    strTemp="user,xp_cmdshell,/add,exec,dbo,xp_cmdshell,localgroup,administrators,<,>,妈的,你妈,狗日,杂种,日庇,做爱,性交,奶子,骚货,靠你妈,操你妈,大鸡巴,性生活,乳房,妈B,妈b,老子,"+
                "select,count,Asc,char,mid,\':,--,;,insert,delete,Server.CreateObject,.Shell,drop,table,update,truncate,set,from";
        arrstr=strTemp.split(",")
        var input=obj.value;
    for (i=0;i<arrstr.length;i++)
    {
      j=obj.value.toLowerCase().indexOf(arrstr[i]);
      if (obj.value!=""&&j>=0)
      {
           alert("含有非法字符:[ "+arrstr[i]+" ] 系统将自行对其进行处理!");
           switch (arrstr[i])
           {
                   case "<":
                      obj.value=input.replace("<","&lt;");
                   break;
                   case ">":
                      obj.value=input.replace(">","&gt;");
                   break;
                   case "--":
                      obj.value=input.replace("--","");
                   break;
                   case ";":
                      obj.value=input.replace(";","");
                   break;
                   case "\'":
                      obj.value=input.replace("\'","\"");
                   break;
                   default://去掉非法的字符
                   obj.value=input.replace(arrstr[i],"");
           }
       obj.focus();
       obj.select();
           return false;
      }
    }
}
//==========================================
switch(chktype)
{
  case "isletter_l"://判断是否由26个英文字母的L小写组成的字符串
   var reg =/^[a-z]+$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("只能输入由26个英文字母的小写组成的字符串。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
  case "isletter_u"://判断是否为由26个英文字母的U大写组成的字符串
   var reg =/^[A-Z]+$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("只能输入由26个英文字母的大写组成的字符串。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
  case "isletter_numeric_"://判断是否由数字、26个英文字母或者下划线组成的字符串
   var reg =/^\w+$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("只能输入由数字、26个英文字母或者下划线组成的字符串。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
   
  case "isnumeric"://判断是否为数字
   var reg = /^\d+(\.\d+)?$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("请输入数字。"+obj.value);
    obj.focus();
    obj.select();
        return false;
   }
   else if(starend[0]!=0&&starend[1]!=0)
   {
      if (obj.value<starend[0]||obj.value>starend[1])
      {
       alert("您输入的是:"+obj.value+"请输入"+starend[0]+"<->"+starend[1]+"之间的数字。");
       obj.focus();
       obj.select();
           return false;
      }
   }
   else if(starend[0]!=0&&starend[1]==0)
   {
      if (obj.value<starend[0])
      {
       alert("您输入的是:"+obj.value+"请输入大于"+starend[0]+"的数字。");
       obj.focus();
       obj.select();
           return false;
      }
   }
   else if(starend[0]==0&&starend[1]!=0)
   {
      if (obj.value>starend[1])
      {
       alert("您输入的是:"+obj.value+"请输入小于"+starend[1]+"的数字。");
       obj.focus();
       obj.select();
           return false;
      }
   }
   break;
   
   case "isdecimal"://匹配小数
    var reg=/^\d+(\.\d+)?$/
    if(obj.value!=""&&!reg.test(obj.value))
    {
           alert("请输入小数。");
       obj.focus();
       obj.select();
           return false;
        }
        break;
       
   case "isnegative"://要求输入负数
    if(obj.value!=""||obj.value>=0)
        {
           alert("请输入一个负数。");
       obj.focus();
       obj.select();
           return false;
        }
        break;
  case "isnumeric_letter"://判断是否由数字和26个英文字母组成的字符串
   var reg =/^[A-Za-z0-9]+$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("只能输入数字和字母组成的字符串。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
  case "isuser"://判断是否为用户名格式
   var reg = /^[^\d\-_][\w\-]*[^\-_]$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("请输入正确的格式。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
  case "ischinese"://判断是否为汉字
   var reg = /^[\u4E00-\u9FA5]*$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("只能输入汉字。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
  case "isemail"://判断是否为邮箱格式
   var reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("请输入正确的邮箱格式。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
   case "ishttp"://判断是否为http格式
   var reg = /^http:\/\/([^\/]+(\.?))+\/?$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("请输入正确http网址格式。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
  case "isdate_-"://判断是否为合法双位日期格式以-分开
   var reg = /^(19|20)\d\d\-(0|1)\d\-(0|1|2|3)\d$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("请输入正确的双位日期格式如:2005-01-01。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
  case "isdate_/"://判断是否为合法双位日期格式以/分开
   var reg = /^(19|20)\d\d\/(0|1)\d\/(0|1|2|3)\d$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("请输入正确的双位日期格式如:2005/01/01。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
  case "isdate_ymd"://判断是否为合法双位日期格式以 年 月  日的形式
   var reg = /^(19|20)\d\d\年(0|1)\d\月(0|1|2|3)\d日$/;
   if(obj.value!=""&&!reg.test(obj.value))
   {
    alert("请输入正确的双位日期格式如:2005年01月11日。");
    obj.focus();
    obj.select();
        return false;
   }
   break;
   
   case "istel"://检查是否为电话号码
    var i,j,strTemp;
    strTemp="0123456789-()# ";
        if (obj.value!=""&&obj.value.length <7)
        {
              alert("请输入不小于7位的电话号码。");
          obj.focus();
          obj.select();
                  return false;
        }
        else if (obj.value!=""&&obj.value.length >15)
        {
              alert("电话号码不能大于15位。");
          obj.focus();
          obj.select();
        }
        else
        {
    for (i=0;i<obj.value.length;i++)
    {
      j=strTemp.indexOf(obj.value.charAt(i));
      if (obj.value!=""&&j==-1)
      {
           alert("请输入正确的电话号码格式。");
       obj.focus();
       obj.select();
           return false;
      }
    }
        }
        break;

   case "ismobiletelephone"://检查是否为手机号码
    var i,j,strTemp;
    strTemp="0123456789";
        if (obj.value!=""&&obj.value.length <11)
        {
              alert("请输入11位的手机号码。");
          obj.focus();
          obj.select();
                  return false;
        }
        else if (obj.value!=""&&obj.value.length >12)
        {
              alert("手机号码不能大于12位。");
          obj.focus();
          obj.select();
        }
        else
        {
       for (i=0;i<obj.value.length;i++)
       {
         j=strTemp.indexOf(obj.value.charAt(i));
         if (j==-1)
         {
              alert("请输入正确的手机号码。");
          obj.focus();
          obj.select();
                  return false;
         }
      }
         }
        break;
       
   case "ispostalcode"://检查是否为邮政编号
    var i,j,strTemp;
    strTemp="0123456789";
        if (obj.value!=""&&obj.value.length != 6)
        {
              alert("请输入6位的邮政编号。");
          obj.focus();
          obj.select();
                  return false;
        }
        else
        {
       for (i=0;i<obj.value.length;i++)
       {
         j=strTemp.indexOf(obj.value.charAt(i));
         if (j==-1)
         {
              alert("请输入正确的邮政编号。");
          obj.focus();
          obj.select();
                  return false;
         }
      }
         }
        break;
       
        case "isip"://匹配IP地址的正则表达式
    var reg=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g
    if(obj.value!=""&&!reg.test(obj.value))
    {
           alert("请输入一个正确的IP地址。");
       obj.focus();
       obj.select();
           return false;
    }
        break;
       
}
}
//为空检查函数
function yn_null(obj,isnull)
{
  for (var loop=0; loop < isnull.length; loop++)
  {
        if (isnull[loop]>=0)
        {
      if (obj.elements[isnull[loop]].value == "")
      {
      alert("对不起,请输入数据!")
          obj.elements[isnull[loop]].focus();
          obj.elements[isnull[loop]].select();
          return false;
      }
        }
  }       
}
//兼容netscape
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//================================================================================

wingbird 发表于 2008-6-28 21:28

使用例子摘录

//=====================
<form id="upMoreFile" method="post" encType="multipart/form-data"   runat="server" onSubmit="return CHKInput(this,1,[-1,-1,-1,3,-1,-1,-1,-1,-1,-1],'0',0,[0,0],0)">

  <asp:TextBox ID="Subject" runat="server"  onBlur=" CHKInput(this,0,[-1],'0',1,[0,0],0)" />
//===========================


<%@ Page language="C#"  AutoEventWireup="false"  Inherits="HCIDAOA.Page.Addarrange"  %>
<HTML>
<HEAD>
<title>事件对话框</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
body {
    font-size:0pt;
    background-color: #FFFFFF;
        margin-left: 0px;
        margin-top: 0px;
        margin-right: 1px;
        margin-bottom: 0px;
}
-->
</style>
</HEAD>
<link rel="stylesheet" href="/styles/css.css" type="text/css">
<script language="javascript" src="/js/calendar.js"></script>
<script language="JavaScript" src="/js/Security_verify.js"></script>
<body>
<form action="" method="post" Id="Myform" runat="server" onSubmit="return CHKInput(this,1,[-1,1,-1,3,-1,-1,6,-1,-1,9],'0',0,[0,0],0)">
  <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="6" height="28" scope="col"><img src="../images/4_03.gif" alt="n" width="6" height="28"></td>
      <td width="0" height="28" align="left" background="../images/4_04.gif" scope="col">事件内容</td>
      <td width="6" height="28" scope="col"><img src="../images/4_05.gif" alt="n" width="6" height="28"></td>
    </tr>
  </table>
  <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="4" rowspan="2" background="../images/4_07.gif" scope="col"><img src="../images/4_07.gif" height="0" alt="n" width="4"></td>
      <td height="0" align="center" valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF" scope="col"><table width="100%" cellspacing="1" bgcolor="#FFFFFF">
        <tr>
          <td bgcolor="#E7EFFF">主 &nbsp;&nbsp;&nbsp;题:</td>
          <td bgcolor="#E7EFFF"><asp:TextBox ID="Subject" runat="server"  onBlur=" CHKInput(this,0,[-1],'0',1,[0,0],0)" />      
          </td>
        </tr>
        <tr>
          <td bgcolor="#E7EFFF">优先级别:</td>
          <td bgcolor="#E7EFFF"><asp:DropDownList ID="Priority" runat="server" Width="200">
              <asp:ListItem Value="高">高</asp:ListItem>
              <asp:ListItem Value="中">中</asp:ListItem>
              <asp:ListItem Value="低">低</asp:ListItem>
          </asp:DropDownList>
            <asp:TextBox ID="id" Width="1" Visible="false"  Height="1" BackColor="#999999" BorderColor="#999999" ForeColor="#999999" runat="server" /></td>
        </tr>
        <tr>
          <td bgcolor="#E7EFFF">开始时间:</td>
          <td bgcolor="#E7EFFF"><asp:TextBox ID="Start_time" runat="server"  onBlur=" CHKInput(this,0,[-1],'isdate_-',1,[0,0],0)" />      
              <a onMouseOver="window.status='日期控件';return true;" onMouseOut="window.status='';return true;" href="javascript:show_calendar('Myform.Start_time');" border="0"><img src="../images/b_time.gif" alt="选择日期" width="34" height="21" border="0" align="absMiddle"></a>
              <asp:DropDownList ID="Start_hour" runat="server" Width="60">
                <asp:ListItem Value="00">00</asp:ListItem>
                <asp:ListItem Value="01">01</asp:ListItem>
                <asp:ListItem Value="02">02</asp:ListItem>
                <asp:ListItem Value="03">03</asp:ListItem>
                <asp:ListItem Value="04">04</asp:ListItem>
                <asp:ListItem Value="05">05</asp:ListItem>
                <asp:ListItem Value="06">06</asp:ListItem>
                <asp:ListItem Value="07">07</asp:ListItem>
                <asp:ListItem Value="08">08</asp:ListItem>
                <asp:ListItem Value="09">09</asp:ListItem>
                                <asp:ListItem Value="10">10</asp:ListItem>
                <asp:ListItem Value="11">11</asp:ListItem>
                <asp:ListItem Value="12">12</asp:ListItem>
                <asp:ListItem Value="13">13</asp:ListItem>
                <asp:ListItem Value="14">14</asp:ListItem>
                <asp:ListItem Value="15">15</asp:ListItem>
                <asp:ListItem Value="16">16</asp:ListItem>
                <asp:ListItem Value="17">17</asp:ListItem>
                <asp:ListItem Value="18">18</asp:ListItem>
                <asp:ListItem Value="19">19</asp:ListItem>
                <asp:ListItem Value="20">20</asp:ListItem>
                <asp:ListItem Value="21">21</asp:ListItem>
                <asp:ListItem Value="22">22</asp:ListItem>
                <asp:ListItem Value="23">23</asp:ListItem>
              </asp:DropDownList>
            :
            <asp:DropDownList ID="Start_minute" runat="server" Width="60">
                        </asp:DropDownList>
                        </td>
        </tr>
        <tr>
          <td bgcolor="#E7EFFF">&nbsp;结束时间:</td>
          <td bgcolor="#E7EFFF"><asp:TextBox ID="End_time" runat="server"  onBlur=" CHKInput(this,0,[-1],'isdate_-',1,[0,0],0)" />      
              <a onMouseOver="window.status='日期控件';return true;" onMouseOut="window.status='';return true;" href="javascript:show_calendar('Myform.End_time');" border="0"><img src="../images/b_time.gif" alt="选择日期" width="34" height="21" border="0" align="absMiddle"></a>
              <asp:DropDownList ID="End_hour" runat="server" Width="60">
                <asp:ListItem Value="00">00</asp:ListItem>
                <asp:ListItem Value="01">01</asp:ListItem>
                <asp:ListItem Value="02">02</asp:ListItem>
                <asp:ListItem Value="03">03</asp:ListItem>
                <asp:ListItem Value="04">04</asp:ListItem>
                <asp:ListItem Value="05">05</asp:ListItem>
                <asp:ListItem Value="06">06</asp:ListItem>
                <asp:ListItem Value="07">07</asp:ListItem>
                <asp:ListItem Value="08">08</asp:ListItem>
                <asp:ListItem Value="09">09</asp:ListItem>
                                <asp:ListItem Value="10">10</asp:ListItem>
                <asp:ListItem Value="11">11</asp:ListItem>
                <asp:ListItem Value="12">12</asp:ListItem>
                <asp:ListItem Value="13">13</asp:ListItem>
                <asp:ListItem Value="14">14</asp:ListItem>
                <asp:ListItem Value="15">15</asp:ListItem>
                <asp:ListItem Value="16">16</asp:ListItem>
                <asp:ListItem Value="17">17</asp:ListItem>
                <asp:ListItem Value="18">18</asp:ListItem>
                <asp:ListItem Value="19">19</asp:ListItem>
                <asp:ListItem Value="20">20</asp:ListItem>
                <asp:ListItem Value="21">21</asp:ListItem>
                <asp:ListItem Value="22">22</asp:ListItem>
                <asp:ListItem Value="23">23</asp:ListItem>
              </asp:DropDownList>
            :
            <asp:DropDownList ID="End_minute" runat="server" Width="60">
                        </asp:DropDownList>
                        </td>
        </tr>
        <tr>
          <td bgcolor="#E7EFFF"> 备忘:</td>
          <td bgcolor="#E7EFFF"><asp:TextBox ID="Memo" TextMode="MultiLine" runat="server" Rows="8" Columns="50" /></td>
        </tr>
        <tr>
          <td align="center" bgcolor="#E7EFFF"><asp:CheckBox ID="Remind" runat="server" />         
提醒:</td>
          <td bgcolor="#E7EFFF">提前:
            <asp:DropDownList ID="Ahead" runat="server" Width="60">
                <asp:ListItem Value="0分钟">0分钟</asp:ListItem>
                <asp:ListItem Value="5分钟">5分钟</asp:ListItem>
            </asp:DropDownList>
分钟 <asp:CheckBox ID="tooa" runat="server" Text='提醒到OA短信' />
<asp:CheckBox ID="tosj" runat="server" Text='提醒到手机' />
<asp:CheckBox ID="toemail" runat="server" Text='提醒到Email' /></td></tr>
        <tr align="center" valign="middle">
          <td colspan="2" bgcolor="#E7EFFF"><asp:Button Class="bu" ID="Send" runat="server" Text="" /></td>
        </tr>
      </table></td>
      <td width="4" height="0" rowspan="2" background="../images/4_17.gif" scope="col"><img src="../images/4_17.gif" height="0" alt="n" width="4"></td>
    </tr>
  </table>
  <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="4" height="4" scope="col"><img src="../images/4_22.gif" alt="n" width="4" height="4"></td>
      <td background="../images/4_23.gif" scope="col"><img src="../images/4_23.gif" alt="n" width="0" height="4"></td>
      <td width="4" height="4" scope="col"><img src="../images/4_24.gif" alt="n" width="4" height="4"></td>
    </tr>
  </table>
</form>
</BODY>
</HTML>

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.