• PageHelper 类 和 ValidateHelper 类


    PageHelper 类

    View Code

      1  public class PageRequest
    2 {
    3 /// <summary>
    4 /// 从地址栏获取值
    5 /// </summary>
    6 /// <param name="page">页面</param>
    7 /// <param name="para">参数</param>
    8 /// <returns></returns>
    9 public static string GetQueryString(string para)
    10 {
    11 string queryString = "";
    12 if (System.Web.HttpContext.Current.Request.QueryString[para] != null)
    13 {
    14 queryString = System.Web.HttpContext.Current.Request.QueryString[para].ToString();
    15 }
    16 else
    17 {
    18 queryString = "";
    19 }
    20 return queryString.Trim();
    21 }
    22
    23 /// <summary>
    24 /// 从地址栏获取相关int值
    25 /// </summary>
    26 /// <param name="page">页面</param>
    27 /// <param name="para">参数</param>
    28 /// <returns></returns>
    29 public static int GetQueryInt(string para)
    30 {
    31 int queryInt = -1;
    32 string tempQueryString = GetQueryString(para);
    33 if (ValidateHelper.IsNumber(tempQueryString))
    34 {
    35 queryInt = int.Parse(tempQueryString);
    36 }
    37 return queryInt;
    38 }
    39
    40 /// <summary>
    41 /// 从表单项获取相关值
    42 /// </summary>
    43 /// <param name="page">页面</param>
    44 /// <param name="para">参数</param>
    45 /// <returns></returns>
    46 public static string GetFormString(string para)
    47 {
    48 string formString = "";
    49 if (System.Web.HttpContext.Current.Request.Form[para] != null)
    50 {
    51 formString = System.Web.HttpContext.Current.Request.Form[para].ToString();
    52 }
    53 else
    54 {
    55 formString = "";
    56 }
    57 return formString.Trim();
    58 }
    59
    60 /// <summary>
    61 /// 从表单获取Int类型的值
    62 /// </summary>
    63 /// <param name="page">页面</param>
    64 /// <param name="para">参数</param>
    65 /// <returns></returns>
    66 public static int GetFormInt(string para)
    67 {
    68 int formInt = -1;
    69 string tempFormString = GetFormString(para);
    70 if (ValidateHelper.IsNumber(tempFormString))
    71 {
    72 formInt = int.Parse(tempFormString);
    73 }
    74 return formInt;
    75 }
    76
    77 /// <summary>
    78 /// 从页面中获取string类型值
    79 /// </summary>
    80 /// <param name="page">页面</param>
    81 /// <param name="para">参数</param>
    82 /// <returns></returns>
    83 public static string GetString(string para)
    84 {
    85 if (System.Web.HttpContext.Current.Request.RequestType.ToLower().Equals("get"))
    86 {
    87 return GetQueryString(para);
    88 }
    89 else
    90 {
    91 return GetFormString(para);
    92 }
    93 }
    94
    95 /// <summary>
    96 /// 从页面中获取Int类型的值
    97 /// </summary>
    98 /// <param name="page">页面</param>
    99 /// <param name="para">参数</param>
    100 /// <returns></returns>
    101 public static int GetInt(string para)
    102 {
    103 string tempInt = GetString(para);
    104 int intReturn = -1;
    105 if (ValidateHelper.IsNumber(tempInt))
    106 {
    107 intReturn = int.Parse(tempInt);
    108 }
    109 return intReturn;
    110 }
    111
    112 /// <summary>
    113 /// 从参数中获取相关参数数组
    114 /// </summary>
    115 /// <param name="page"></param>
    116 /// <returns></returns>
    117 public static Hashtable GetQueryPara()
    118 {
    119 int paraCount = System.Web.HttpContext.Current.Request.QueryString.Count;
    120 string queryKey = "";
    121 string queryValue = "";
    122 Hashtable para = new Hashtable();
    123 for (int i = 0; i < paraCount; i++)
    124 {
    125 queryKey = System.Web.HttpContext.Current.Request.QueryString.Keys[i].ToString();
    126 queryValue = System.Web.HttpContext.Current.Request.QueryString[i].ToString();
    127 if (!queryKey.Equals("pageindex"))
    128 {
    129 para.Add(queryKey, queryValue);
    130 }
    131 }
    132 return para;
    133
    134 }
    135 /// <summary>
    136 /// 从表单中获取相关参数数组
    137 /// </summary>
    138 /// <param name="page"></param>
    139 /// <returns></returns>
    140 public static Hashtable GetFormPara()
    141 {
    142 int paraCount = System.Web.HttpContext.Current.Request.Form.Count;
    143 string formKey = "";
    144 string formValue = "";
    145 Hashtable para=new Hashtable();
    146 for (int i = 0; i < paraCount; i++)
    147 {
    148 formKey = System.Web.HttpContext.Current.Request.Form.Keys[i].ToString();
    149 formValue = System.Web.HttpContext.Current.Request.Form[i].ToString();
    150 if (!formKey.Equals("pageindex"))
    151 {
    152 para.Add(formKey, formValue);
    153 }
    154 }
    155
    156 return para;
    157
    158 }
    159
    160 /// <summary>
    161 /// 获取参数数组
    162 /// </summary>
    163 /// <param name="page"></param>
    164 /// <returns></returns>
    165 public static Hashtable GetPara()
    166 {
    167 if (System.Web.HttpContext.Current.Request.RequestType.ToLower().Equals("get"))
    168 {
    169 return GetQueryPara();
    170 }
    171 else
    172 {
    173 return GetFormPara();
    174 }
    175 }
    176 }

    ValidateHelper 类

    View Code

    /// <summary>
    /// 数据验证
    /// </summary>
    public class ValidateHelper
    {
    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]+$");
    private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
    private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");

    #region 数字字符串检查

    /// <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;
    }

    #endregion

    #region 中文检测

    /// <summary>
    /// 检测是否有中文字符
    /// </summary>
    /// <param name="inputData"></param>
    /// <returns></returns>
    public static bool IsHasCHZN(string inputData)
    {
    Match m = RegCHZN.Match(inputData);
    return m.Success;
    }

    #endregion

    #region 邮件地址
    /// <summary>
    /// 是否是浮点数 可带正负号
    /// </summary>
    /// <param name="inputData">输入字符串</param>
    /// <returns></returns>
    public static bool IsEmail(string inputData)
    {
    Match m = RegEmail.Match(inputData);
    return m.Success;
    }

    #endregion

    #region 是否是时间格式
    /// <summary>
    /// 判断一个字符串是否时间格式
    /// </summary>
    /// <param name="inputData">输入字符串</param>
    /// <returns></returns>
    public static bool IsDateTime(string inputData)
    {
    try
    {
    Convert.ToDateTime(inputData);
    return true;
    }
    catch
    {
    return false;
    }
    }

    #endregion
    }



  • 相关阅读:
    1 外部JavaScript
    1 外部JavaScript
    蓝桥杯:位运算
    Java为什么要配置环境变量
    java数组静态复制和动态复制越界问题
    Dijkstra算法java实现
    离散二元关系实验java实现
    Java中字符串split() 的使用方法
    用java编程实现集合的交、并、差和补运算
    61根据字符出现频率排序(451)
  • 原文地址:https://www.cnblogs.com/cnscpz/p/2209888.html
Copyright © 2020-2023  润新知