2008-04-10 10:24
放公共类
如:SQLHelper.cs数据访问抽象基础类 ControlBindHelper.cs 控件帮定抽象基础类 DataBase.cs 操作控件类 MessageBox.cs 显示消息提示对话框类。 PageValidate.cs 页面数据校验类
//****************************PageValidate.cs类 代码
using System;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace CoalTraffic.Common
{
///
/// 页面数据校验类
///
/// 2007.8
///
public class PageValidate
{
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegLetter = new Regex("^[a-zA-Z]+$");
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]+$"); //等价于^[+-]?/d+[.]?/d+$
private static Regex RegEmail = new Regex("^[//w-]+@[//w-]+//.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
private static Regex RegCHZN = new Regex("[/u4e00-/u9fa5]");
private static Regex RegDate = new Regex("^( //d{4})(///|-)(//d{1,2})(///|-)(//d{1,2})$");
public PageValidate()
{
}
#region 数字字符串检查
///
/// 检查Request查询字符串的键值,是否是数字,最大长度限制
///
/// Request
/// Request的键值
/// 最大长度
/// 返回Request查询字符串
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;
}
///
/// 判断字符串是否大于指定长度
///
/// 要比较的字符串
/// 指定长度
///
public static bool IsLengther(string inputData, int imaxLen)
{
bool blFlag = false;
int iLength = inputData.Length;
if (iLength > imaxLen)
{
blFlag = true;
}
else
{
blFlag = false;
}
return blFlag;
}
///
/// 判断字符串是否小于指定长度
///
/// 要比较的字符串
/// 指定长度
///
public static bool IsShorter(string inputData, int iminLen)
{
bool blFlag = false;
int iLength = inputData.Length;
if (iLength < iminLen)
{
blFlag = true;
}
else
{
blFlag = false;
}
return blFlag;
}
///
/// 是否数字字符串
///
/// 输入字符串
///
public static bool IsNumber(string inputData)
{
Match m = RegNumber.Match(inputData);
return m.Success;
}
public static bool IsLetter(string inputData)
{
Match m = RegLetter.Match(inputData);
return m.Success;
}
///
/// 是否数字字符串 可带正负号
///
/// 输入字符串
///
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
}
///
/// 是否是浮点数
///
/// 输入字符串
///
public static bool IsDecimal(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
}
///
/// 是否是浮点数 可带正负号
///
/// 输入字符串
///
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecimalSign.Match(inputData);
return m.Success;
}
#endregion
#region 中文检测
///
/// 检测是否有中文字符
///
///
///
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
#endregion
#region 邮件地址
///
/// 是否是浮点数 可带正负号
///
/// 输入字符串
///
public static bool IsEmail(string inputData)
{
Match m = RegEmail.Match(inputData);
return m.Success;
}
#endregion
#region 日期
///
/// 是否是日期格式
///
///
///
public static bool IsDateTime(string inputData)
{
bool flag = false;
string regex = @"^((/d{2}(([02468][048])|([13579][26]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|([1-2][0-9])))))|(/d{2}(([02468][1235679])|([13579][01345789]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"; //日期部分
regex += @"(/s(((0?[0-9])|([1][0-9])|([2][0-4]))/:([0-5]?[0-9])((/s)|(/:([0-5]?[0-9])))))?$"; //时间部分
RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
Regex reg = new Regex(regex, options);
if (reg.IsMatch(inputData))
{
flag = true;
}
return flag;
}
public static bool IsDate(string inputData)
{
bool flag = false;
string regex = @"^((/d{2}(([02468][048])|([13579][26]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|([1-2][0-9])))))|(/d{2}(([02468][1235679])|([13579][01345789]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"; //日期部分
RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
Regex reg = new Regex(regex, options);
if (reg.IsMatch(inputData))
{
flag = true;
}
return flag;
}
#endregion
#region 其他
///
/// 检查字符串最大长度,返回指定长度的串
///
/// 输入字符串
/// 最大长度
///
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;
}
///
/// 字符串编码
///
///
///
public static string HtmlEncode(string inputData)
{
return HttpUtility.HtmlEncode(inputData);
}
///
/// 设置Label显示Encode的字符串
///
///
///
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();
}
///
/// 转换成 HTML code
///
/// string
/// string
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","
");
return str;
}
///
///解析html成 普通文本
///
/// string
/// string
public static string Decode(string str)
{
str = str.Replace("
","/n");
str = str.Replace(">",">");
str = str.Replace("<","<");
str = str.Replace(" "," ");
str = str.Replace(""","/"");
return str;
}
#endregion
}
}