242 lines
5.8 KiB
C#
242 lines
5.8 KiB
C#
|
|
using System;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Web;
|
|||
|
|
using System.Web.UI.WebControls;
|
|||
|
|
using System.Text.RegularExpressions;
|
|||
|
|
|
|||
|
|
namespace ZWL.Common
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// ҳ<><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
|
|||
|
|
/// <20><>Ϊ<EFBFBD><CEAA>
|
|||
|
|
/// 2008.4
|
|||
|
|
/// </summary>
|
|||
|
|
public class PageValidate
|
|||
|
|
{
|
|||
|
|
private static Regex RegNumber = new Regex("^[0-9]+$");
|
|||
|
|
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
|
|||
|
|
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
|
|||
|
|
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //<2F>ȼ<EFBFBD><C8BC><EFBFBD>^[+-]?\d+[.]?\d+$
|
|||
|
|
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w Ӣ<><D3A2><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [a-zA-Z0-9] <20>һ<EFB7A8><D2BB>
|
|||
|
|
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
|
|||
|
|
|
|||
|
|
public PageValidate()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Request<73><74>ѯ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ļ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><F3B3A4B6><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="req">Request</param>
|
|||
|
|
/// <param name="inputKey">Request<73>ļ<EFBFBD>ֵ</param>
|
|||
|
|
/// <param name="maxLen"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns><3E><><EFBFBD><EFBFBD>Request<73><74>ѯ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></returns>
|
|||
|
|
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>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsNumber(string inputData)
|
|||
|
|
{
|
|||
|
|
Match m = RegNumber.Match(inputData);
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> <20>ɴ<EFBFBD><C9B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsNumberSign(string inputData)
|
|||
|
|
{
|
|||
|
|
Match m = RegNumberSign.Match(inputData);
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsDecimal(string inputData)
|
|||
|
|
{
|
|||
|
|
Match m = RegDecimal.Match(inputData);
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD> <20>ɴ<EFBFBD><C9B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsDecimalSign(string inputData)
|
|||
|
|
{
|
|||
|
|
Match m = RegDecimalSign.Match(inputData);
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsHasCHZN(string inputData)
|
|||
|
|
{
|
|||
|
|
Match m = RegCHZN.Match(inputData);
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>ַ
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD> <20>ɴ<EFBFBD><C9B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsEmail(string inputData)
|
|||
|
|
{
|
|||
|
|
Match m = RegEmail.Match(inputData);
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<F3B3A4B6><C8A3><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵĴ<C8B5>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="sqlInput"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></param>
|
|||
|
|
/// <param name="maxLength"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string SqlText(string sqlInput, int maxLength)
|
|||
|
|
{
|
|||
|
|
if(sqlInput != null && sqlInput != string.Empty)
|
|||
|
|
{
|
|||
|
|
sqlInput = sqlInput.Trim();
|
|||
|
|
if(sqlInput.Length > maxLength)//<2F><><EFBFBD><EFBFBD><EFBFBD>Ƚ<F3B3A4B6>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
sqlInput = sqlInput.Substring(0, maxLength);
|
|||
|
|
}
|
|||
|
|
return sqlInput;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string HtmlEncode(string inputData)
|
|||
|
|
{
|
|||
|
|
return HttpUtility.HtmlEncode(inputData);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Label<65><6C>ʾEncode<64><65><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="lbl"></param>
|
|||
|
|
/// <param name="txtInput"></param>
|
|||
|
|
public static void SetLabel(Label lbl, string txtInput)
|
|||
|
|
{
|
|||
|
|
lbl.Text = HtmlEncode(txtInput);
|
|||
|
|
}
|
|||
|
|
public static void SetLabel(Label lbl, object inputObj)
|
|||
|
|
{
|
|||
|
|
SetLabel(lbl, inputObj.ToString());
|
|||
|
|
}
|
|||
|
|
//<2F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
public static string InputText(string inputString, int maxLength)
|
|||
|
|
{
|
|||
|
|
StringBuilder retVal = new StringBuilder();
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
|
|||
|
|
if ((inputString != null) && (inputString != String.Empty))
|
|||
|
|
{
|
|||
|
|
inputString = inputString.Trim();
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>鳤<EFBFBD><E9B3A4>
|
|||
|
|
if (inputString.Length > maxLength)
|
|||
|
|
inputString = inputString.Substring(0, maxLength);
|
|||
|
|
|
|||
|
|
//<2F>滻Σ<E6BBBB><CEA3><EFBFBD>ַ<EFBFBD>
|
|||
|
|
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("'", " ");// <20>滻<EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
}
|
|||
|
|
return retVal.ToString();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ת<><D7AA><EFBFBD><EFBFBD> HTML code
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="str">string</param>
|
|||
|
|
/// <returns>string</returns>
|
|||
|
|
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>
|
|||
|
|
///<2F><><EFBFBD><EFBFBD>html<6D><6C> <20><>ͨ<EFBFBD>ı<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="str">string</param>
|
|||
|
|
/// <returns>string</returns>
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|