在ASP.NET中实现通用的有效性校验2011-10-25 富盛软件 不管在B/S还是C/S中,数据操纵维护与检索,很多时候,都需要判断用户录入信息的有效性。在B/S结 构中,可能还要考虑Sql注入的安全性问题。既然问题很普遍,我们就应该对该问题进行业务抽象,得到统一的处理方案。在此介绍我们的处理方 式,用于实现在ASP.NET中使用C#语言实现的通用有效性检验。我们将整个处理方案分为三部分:1、格式化数据有效性判断,这一部分应该致力做到框架结构的无关性,也就是说,应该做到不管是 C/S结构还是B/S结构,都应该是通用的;数据安全性判断,既然是安全性考虑,原则上B/S和C/S结构也是 通用的,比如Sql 注入的安全性;2、处于友好的交互性和友好的接口,提供一个容易理解的调用接口和非法提醒是必要的。这是构架相 关的,在Web开发和WinForm开发各自有所不同的处理方式。既然标题是ASP.NET中的有效性检验,我们只 讨论Web方式的接口。3、页面调用,既然是页面调用,当然是基于Web的。第一部分:框架通用性对于框架无关的部分,应该考虑放在一个基类中,我们也是这样做的,不妨我们放在积累命名空间为 fsCSharpDevFrk.Common的fsDevCommon类中。其中实现格式话数据有效性判断的方法如下:
    public bool CheckValidValue(string strValue, string strTypeFormat)    {      bool bReturn = true;        if (strTypeFormat != null && strTypeFormat.Length > 0)      {        if (strTypeFormat.StartsWith("D"))        {          string[] strTypes = strTypeFormat.Split(",");          if (strTypes.Length > 1)          {            DateTime dtValue = new DateTime(1900, 1, 1), dtMaxValue = new DateTime(2050, 12, 31);            if (strTypes.Length > 2 && IsDate(strTypes[2]))              dtValue = DateTime.Parse(strTypes[2]);            if (strTypes.Length > 3 && IsDate(strTypes[3]))              dtMaxValue = DateTime.Parse(strTypes[3]);              if (strTypes[1].Trim() == ">")              bReturn = IsDigital(strValue) && (DateTime.Parse(strValue) > dtValue);            else if (strTypes[1].Trim() == ">=")              bReturn = IsDigital(strValue) && (DateTime.Parse(strValue) >= dtValue);            else if (strTypes[1].Trim() == "<")              bReturn = IsDigital(strValue) && (DateTime.Parse(strValue) < dtValue);            else if (strTypes[1].Trim() == "<=")              bReturn = IsDigital(strValue) && (DateTime.Parse(strValue) < dtValue);            else if (strTypes[1].Trim() == "=")              bReturn = IsDigital(strValue) && (DateTime.Parse(strValue) == dtValue);            else if (strTypes[1].Trim().ToUpper() == "BETWEEN")              bReturn = IsDigital(strValue) && (DateTime.Parse(strValue) >= dtValue) && (DateTime.Parse(strValue) <= dtMaxValue);          }          else            bReturn = IsDate(strValue);        }        else if (strTypeFormat.StartsWith("T"))        {          string[] strTypes = strTypeFormat.Split(",");          if (strTypes.Length > 1)          {            DateTime dtValue = new DateTime(1900, 1, 1), dtMaxValue = new DateTime(2050, 12, 31);            if (strTypes.Length > 2 && IsDateTime(strTypes[2]))              dtValue = DateTime.Parse(strTypes[2]);            if (strTypes.Length > 3 && IsDateTime(strTypes[3]))              dtMaxValue = DateTime.Parse(strTypes[3]);              if (strTypes[1].Trim() == ">")              bReturn = IsDateTime(strValue) && (DateTime.Parse(strValue) > dtValue);            else if (strTypes[1].Trim() == ">=")              bReturn = IsDateTime(strValue) && (DateTime.Parse(strValue) >= dtValue);            else if (strTypes[1].Trim() == "<")              bReturn = IsDateTime(strValue) && (DateTime.Parse(strValue) < dtValue);            else if (strTypes[1].Trim() == "<=")              bReturn = IsDateTime(strValue) && (DateTime.Parse(strValue) < dtValue);            else if (strTypes[1].Trim() == "=")              bReturn = IsDateTime(strValue) && (DateTime.Parse(strValue) == dtValue);            else if (strTypes[1].Trim().ToUpper() == "BETWEEN")              bReturn = IsDateTime(strValue) && (DateTime.Parse(strValue) >= dtValue) && (DateTime.Parse(strValue) <= dtMaxValue);          }          else            bReturn = IsDateTime(strValue);        }        else if (strTypeFormat.StartsWith("N"))        {          string[] strTypes = strTypeFormat.Split(",");          if (strTypes.Length > 1)          {            long lValue = 0, lMaxValue = 99999999999999;            if (strTypes.Length > 2 && IsDigital(strTypes[2]))              lValue = long.Parse(strTypes[2]);            if (strTypes.Length > 3 && IsDigital(strTypes[3]))              lMaxValue = long.Parse(strTypes[3]);              if (strTypes[1].Trim() == ">")              bReturn = IsDigital(strValue) && (int.Parse(strValue) > lValue);            else if (strTypes[1].Trim() == ">=")              bReturn = IsDigital(strValue) && (int.Parse(strValue) >= lValue);            else if (strTypes[1].Trim() == "<")              bReturn = IsDigital(strValue) && (int.Parse(strValue) < lValue);            else if (strTypes[1].Trim() == "<=")              bReturn = IsDigital(strValue) && (int.Parse(strValue) < lValue);            else if (strTypes[1].Trim() == "=")              bReturn = IsDigital(strValue) && (int.Parse(strValue) == lValue);            else if (strTypes[1].Trim().ToUpper() == "BETWEEN")              bReturn = IsDigital(strValue) && (int.Parse(strValue) >= lValue) && (int.Parse(strValue) <= lMaxValue);          }          else            bReturn = IsDigital(strValue);        }        else if (strTypeFormat.StartsWith("F"))        {          string[] strTypes = strTypeFormat.Split(",");          if (strTypes.Length > 1)          {            double dValue = 0, dMaxValue = 999999999;            if (strTypes.Length > 2 && IsNumeric(strTypes[2]))              dValue = double.Parse(strTypes[2]);            if (strTypes.Length > 3 && IsDigital(strTypes[3]))              dMaxValue = long.Parse(strTypes[3]);              if (strTypes[1].Trim() == ">")              bReturn = IsNumeric(strValue) && (double.Parse(strValue) > dValue);            else if (strTypes[1].Trim() == ">=")              bReturn = IsNumeric(strValue) && (double.Parse(strValue) >= dValue);            else if (strTypes[1].Trim() == "<")              bReturn = IsNumeric(strValue) && (double.Parse(strValue) < dValue);            else if (strTypes[1].Trim() == "<=")              bReturn = IsNumeric(strValue) && (double.Parse(strValue) < dValue);            else if (strTypes[1].Trim() == "=")              bReturn = IsNumeric(strValue) && (double.Parse(strValue) == dValue);            else if (strTypes[1].Trim().ToUpper() == "BETWEEN")              bReturn = IsNumeric(strValue) && (double.Parse(strValue) >= dValue) && (double.Parse(strValue) <= dMaxValue);          }          else            bReturn = IsNumeric(strValue);        }        else if (strTypeFormat.StartsWith("C"))        {          string[] strTypes = strTypeFormat.Split(",");          if (strTypes.Length > 1)          {            string strVal = "", strMaxValue = "";            if (strTypes.Length > 2 && strTypes[2] != null && strTypes[2].Length > 0)              strVal = strTypes[2];            if (strTypes.Length > 3 && strTypes[3] != null && strTypes[3].Length > 0)              strMaxValue = strTypes[3];              if (strTypes[1].Trim() == ">")              bReturn = IsNumeric(strValue) && (strValue.CompareTo(strVal) > 0);            else if (strTypes[1].Trim() == ">=")              bReturn = IsNumeric(strValue) && (strValue.CompareTo(strVal) >= 0);            else if (strTypes[1].Trim() == "<")              bReturn = IsNumeric(strValue) && (strValue.CompareTo(strVal) < 0);            else if (strTypes[1].Trim() == "<=")              bReturn = IsNumeric(strValue) && (strValue.CompareTo(strVal) <= 0);            else if (strTypes[1].Trim() == "=")              bReturn = IsNumeric(strValue) && (strValue.CompareTo(strVal) == 0);            else if (strTypes[1].Trim().ToUpper() == "BETWEEN")              bReturn = IsNumeric(strValue) && (strValue.CompareTo(strVal) >= 0) && (strValue.CompareTo(strMaxValue) <= 0);            else if (strTypes[1].Trim().ToUpper() == "IN")            {              if (strVal.Length > 0)              {                if (strMaxValue.Length > 0)                  bReturn = IsExists(strValue, StringSplit(strVal, strMaxValue));                else                  bReturn = strValue.IndexOf(strVal) >= 0;              }              else                bReturn = false;            }          }        }      }        return bReturn;    }