.NET页面数据验证通用类
- public class PageValidate
- {
- private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]contentquot;);
- private static Regex RegNumber = new Regex("^[0-9]+contentquot;);
- private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+contentquot;);
- private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+contentquot;);
- private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+contentquot;);
- private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)contentquot;);
- private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
-
- public PageValidate()
- {
- }
-
-
-
- public static bool IsPhone(string inputData)
- {
- Match m = RegPhone.Match(inputData);
- return m.Success;
- }
- /// <summary>
-
-
-
-
-
-
- public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
- {
- string retVal = string.Empty;
- if(inputKey != null && inputKey != string.Empty)
- {
- retVal = req.QueryString[inputKey];
- if(null == retVal)
- retVal = req.Form[inputKey];
- if(null != retVal)
- {
- retVal = SqlText(retVal, maxLen);
- if(!IsNumber(retVal))
- retVal = string.Empty;
- }
- }
- if(retVal == null)
- retVal = string.Empty;
- return retVal;
- }
- /// <summary>
-
-
-
-
- public static bool IsNumber(string inputData)
- {
- Match m = RegNumber.Match(inputData);
- return m.Success;
- }
-
- /// <summary>
-
-
-
-
- public static bool IsNumberSign(string inputData)
- {
- Match m = RegNumberSign.Match(inputData);
- return m.Success;
- }
- /// <summary>
-
-
-
-
- public static bool IsDecimal(string inputData)
- {
- Match m = RegDecimal.Match(inputData);
- return m.Success;
- }
- /// <summary>
-
-
-
-
- public static bool IsDecimalSign(string inputData)
- {
- Match m = RegDecimalSign.Match(inputData);
- return m.Success;
- }
-
- #endregion
-
-
-
- /// <summary>
-
-
-
-
- public static bool IsHasCHZN(string inputData)
- {
- Match m = RegCHZN.Match(inputData);
- return m.Success;
- }
-
- #endregion
-
-
- /// <summary>
-
-
-
-
- public static bool IsEmail(string inputData)
- {
- Match m = RegEmail.Match(inputData);
- return m.Success;
- }
-
- #endregion
-
-
-
- /// <summary>
-
-
-
-
-
- public static string SqlText(string sqlInput, int maxLength)
- {
- if(sqlInput != null && sqlInput != string.Empty)
- {
- sqlInput = sqlInput.Trim();
- if(sqlInput.Length > maxLength)
- sqlInput = sqlInput.Substring(0, maxLength);
- }
- return sqlInput;
- }
- /// <summary>
-
-
-
-
- public static string HtmlEncode(string inputData)
- {
- return HttpUtility.HtmlEncode(inputData);
- }
- /// <summary>
-
-
-
-
- public static void SetLabel(Label lbl, string txtInput)
- {
- lbl.Text = HtmlEncode(txtInput);
- }
- public static void SetLabel(Label lbl, object inputObj)
- {
- SetLabel(lbl, inputObj.ToString());
- }
-
- public static string InputText(string inputString, int maxLength)
- {
- StringBuilder retVal = new StringBuilder();
-
-
- if ((inputString != null) && (inputString != String.Empty))
- {
- inputString = inputString.Trim();
-
-
- if (inputString.Length > maxLength)
- inputString = inputString.Substring(0, maxLength);
-
-
- for (int i = 0; i < inputString.Length; i++)
- {
- switch (inputString[i])
- {
- case '"':
- retVal.Append(""");
- break;
- case '<':
- retVal.Append("<");
- break;
- case '>':
- retVal.Append(">");
- break;
- default:
- retVal.Append(inputString[i]);
- break;
- }
- }
- retVal.Replace("'", " ");
- }
- return retVal.ToString();
-
- }
- /// <summary>
-
-
-
-
- public static string Encode(string str)
- {
- str = str.Replace("&","&");
- str = str.Replace("'","''");
- str = str.Replace("\"",""");
- str = str.Replace(" "," ");
- str = str.Replace("<","<");
- str = str.Replace(">",">");
- str = str.Replace("\n","<br>");
- return str;
- }
- /// <summary>
-
-
-
-
- public static string Decode(string str)
- {
- str = str.Replace("<br>","\n");
- str = str.Replace(">",">");
- str = str.Replace("<","<");
- str = str.Replace(" "," ");
- str = str.Replace(""","\"");
- return str;
- }
-
- public static string SqlTextClear(string sqlText)
- {
- if (sqlText == null)
- {
- return null;
- }
- if (sqlText == "")
- {
- return "";
- }
- sqlText = sqlText.Replace(",", "");
- sqlText = sqlText.Replace("<", "");
- sqlText = sqlText.Replace(">", "");
- sqlText = sqlText.Replace("--", "");
- sqlText = sqlText.Replace("'", "");
- sqlText = sqlText.Replace("\"", "");
- sqlText = sqlText.Replace("=", "");
- sqlText = sqlText.Replace("%", "");
- sqlText = sqlText.Replace(" ", "");
- return sqlText;
- }
- #endregion
-
-
- public static bool isContainSameChar(string strInput)
- {
- string charInput = string.Empty;
- if (!string.IsNullOrEmpty(strInput))
- {
- charInput = strInput.Substring(0, 1);
- }
- return isContainSameChar(strInput, charInput, strInput.Length);
- }
-
- public static bool isContainSameChar(string strInput, string charInput, int lenInput)
- {
- if (string.IsNullOrEmpty(charInput))
- {
- return false;
- }
- else
- {
- Regex RegNumber = new Regex(string.Format("^([{0}])+contentquot;, charInput));
-
- Match m = RegNumber.Match(strInput);
- return m.Success;
- }
- }
- #endregion
-
-
- /// <summary>
-
-
- public static bool isContainSpecChar(string strInput)
- {
- string list = new string { "123456", "654321" };
- bool result = new bool();
- for (int i = 0; i < list.Length; i++)
- {
- if (strInput == list[i])
- {
- result = true;
- break;
- }
- }
- return result;
- }
- #endregion
- }