asp.net页面数据校验类
来源:互联网 发布:十代思域软件安装教程 编辑:程序博客网 时间:2024/05/16 07:15
using System;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions; //引入正则表达式命名空间
namespace Project.Validate
{
/// <summary>
/// PageValidate 页面数据校验类
/// </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]+$"); //等价于^[+-]?/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]");
public PageValidate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 检查Request查询字符串的键值,是否是数字,最大长度限制
/// </summary>
/// <param name="req">Request</param>
/// <param name="inputKey">Request的键值</param>
/// <param name="maxLen">最大长度</param>
/// <returns>返回Request查询字符串</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>
/// 是否数字字符串
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumber(string inputData)
{
Match m = RegNumber.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否数字字符串 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecimalSign.Match(inputData);
return m.Success;
}
/// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsEmail(string inputData)
{
Match m = RegEmail.Match(inputData);
return m.Success;
}
/// <summary>
/// 检查字符串最大长度,返回指定长度的串
/// </summary>
/// <param name="sqlInput">输入字符串</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
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>
/// 字符串编码
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static string HtmlEncode(string inputData)
{
return HttpUtility.HtmlEncode(inputData);
}
/// <summary>
/// 设置Label显示Encode的字符串
/// </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());
}
}
}
- asp.net页面数据校验类
- ASP.Net前台数据校验
- 页面数据校验类
- 页面数据校验类
- asp.net 常用类---页面数据验证
- ASP.NET页面数据验证通用类
- 页面数据校验类[转]
- asp.net 页面传输数据
- 关于asp.net页面添加非空校验控件注意事项
- asp.net页面类
- asp.net前台页面数据绑定----对象类表绑定
- 页面数据校验
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法!
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法
- 关于SiteMapPath的使用
- PostgreSQL安装配置入门
- CSS Hack整理
- 经典的内存池技术
- Linux C 函数参考(文件内容操作)
- asp.net页面数据校验类
- 大富翁论坛-富翁笔记-利用Delphi压缩和修复MS Access 2000的数据库文件
- CMS系统基本概念
- (PHP)海量数据输出CSV文件时由于特殊字符引起的CSV文件格式异常的问题解决
- log4j 日志论
- TABLE ADAPTER中使用通配符
- Hibernate API 变化
- Turbo C 2.0 Borland C++ 库函数 及 用例
- vcl组件精要