不管在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;
}