zoukankan      html  css  js  c++  java
  • 数据验证类

    1 /*
    2 * **********************************************************
    3 * 数据验证类
    4
    5 */
    6
    7
    8  using System;
    9 using System.Collections.Generic;
    10 using System.Text;
    11 using System.Text.RegularExpressions;
    12 using System.Data;
    13
    14
    15 public class ValidateClass
    16 {
    17 /// <summary>
    18 /// 数据验证类
    19 /// </summary>
    20 public ValidateClass() { }
    21
    22 /// <summary>
    23 /// 邮箱地址是否有效
    24 /// </summary>
    25 /// <param name="s">邮箱地址</param>
    26 /// <returns>返回判断结果 true:有效 false:无效</returns>
    27 public bool IsValidEmail(string s)
    28 {
    29 s = s.Trim();
    30 return Regex.IsMatch(s, @"^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$");
    31 }
    32
    33 /// <summary>
    34 /// 网址是否有效
    35 /// </summary>
    36 /// <param name="s">网址</param>
    37 /// <returns>返回判断结果 true:有效 false:无效</returns>
    38 public bool IsValidUrl(string s)
    39 {
    40 s = s.Trim();
    41 return Regex.IsMatch(s, @"(http)|(https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
    42 }
    43
    44 /// <summary>
    45 /// 日期时间格式是否有效
    46 /// yyyy-MM-dd HH:mm:ss
    47 /// </summary>
    48 /// <param name="s">日期时间字符串</param>
    49 /// <returns>返回判断结果 true:有效 false:无效</returns>
    50 public bool IsValidDateTime(string s)
    51 {
    52 s = s.Trim();
    53 return Regex.IsMatch(s, @"^\d{4}\-(\d|\d{2})\-(\d|\d{2}) \d{2}:\d{2}:\d{2}");
    54 }
    55
    56 /// <summary>
    57 /// 日期格式是否有效
    58 /// yyyy-MM-dd
    59 /// </summary>
    60 /// <param name="s">日期字符串</param>
    61 /// <returns>返回判断结果 true:有效 false:无效</returns>
    62 public bool IsValidDate(string s)
    63 {
    64 s = s.Trim();
    65 // return Regex.IsMatch(s, @"^\d{4}\-(\d|\d{2})\-(\d|\d{2})");
    66 return Regex.IsMatch(s, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
    67
    68 }
    69
    70 /// <summary>
    71 /// 时间格式是否有效
    72 /// HH:mm:ss
    73 /// </summary>
    74 /// <param name="s">时间字符串</param>
    75 /// <returns>返回判断结果 true:有效 false:无效</returns>
    76 public bool IsValidTime(string s)
    77 {
    78 s = s.Trim();
    79 return Regex.IsMatch(s, @"^\d{2}:\d{2}:\d{2}");
    80 }
    81
    82 /// <summary>
    83 /// 字符串是否为数字
    84 /// </summary>
    85 /// <param name="s">数字串</param>
    86 /// <returns>返回判断结果 true:是 false:否</returns>
    87 public bool IsNumeric(string s)
    88 {
    89 s = s.Trim();
    90 if (s == "" || s == null)
    91 {
    92 return false;
    93 }
    94 for (int i = 0; i < s.Length; i++)
    95 {
    96 if (!char.IsNumber(s, i))
    97 {
    98 return false;
    99 }
    100 }
    101 return true;
    102 }
    103
    104 public bool IsFloat(string s)
    105 {
    106 s = s.Trim();
    107 return Regex.IsMatch(s, @"^\d+\.\d{0,7}$");
    108 }
    109
    110 /// <summary>
    111 /// 验证身份证(15位或18位)
    112 /// </summary>
    113 /// <param name="str"></param>
    114 /// <returns></returns>
    115 public bool IsIdentityCard(string str)
    116 {
    117
    118 if (str == "")//可以为空
    119 return true;
    120 // return Regex.IsMatch(str, @"~\d{17}[\d|X]|\d{15}");
    121 if (str.Length != 15 && str.Length != 18)
    122 return false;
    123 if (str.Length == 15)
    124 {
    125 return IsNumeric(str);
    126 }
    127 else if (str.Length == 18)
    128 {
    129 str = str.Substring(0, (str.Length - 1));
    130 return IsNumeric(str);
    131 }
    132 else
    133 return false;
    134 //bool aa = true;
    135 //if (str.Length == 15)
    136 //{
    137 // if (str == "111111111111111")
    138 // return false;
    139 // else
    140 // return Regex.IsMatch(str, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
    141 //}
    142 //else if (str.Length == 18)
    143 //{
    144 // if (str == "111111111111111111")//下面正则表达是有buge,当全为1时不检测,所以..
    145 // return false;
    146 // aa = Regex.IsMatch(str, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$");
    147 // string st = str.Substring(17, 1);
    148 // bool bb = Regex.IsMatch(st, @"^[A-Za-z]+$");//最后一位可为字母
    149 // if (aa == false)
    150 // return false;
    151 // else if (aa == true || bb == true)
    152 // return true;
    153 // else
    154 // return false;
    155 //}
    156 //else
    157 // return false;
    158
    159 }
    160 /// <summary>
    161 /// 把DataRow[] 的内容保存到DataTable中,并以DataSet返回,条件DataRow[]中的所有字段是和DataTable一样的
    162 /// </summary>
    163 /// <param name="dt"></param>
    164 /// <param name="dr"></param>
    165 /// <returns></returns>
    166 public DataSet GetDataSetByDTAndDR(DataTable dt, DataRow[] dr)
    167 {
    168 DataSet ds = new DataSet();
    169
    170 DataTable returnDT = new DataTable();
    171
    172 returnDT = dt.Clone();
    173
    174 for (int i = 0; i < dr.Length; i++)
    175 {
    176 DataRow dataRow = returnDT.NewRow();
    177
    178 //dr[i].ItemArray.CopyTo(dataRow.ItemArray,0);
    179 for (int j = 0; j < returnDT.Columns.Count; j++)
    180 {
    181 dataRow[returnDT.Columns[j].ColumnName] = dr[i][returnDT.Columns[j].ColumnName];
    182 }
    183
    184 returnDT.Rows.Add(dataRow);
    185 }
    186
    187 ds.Tables.Add(returnDT);
    188
    189 return ds;
    190 }
    191
    192
    193
    194 } // End class ValidateClass
    195
    196
  • 相关阅读:
    【C/C++】qsort函数的使用方法和细节
    MOOC C++笔记(五):继承
    MOOC 数据库系统笔记(二):数据库系统的基本结构及其演变发展
    PTA A1015
    MOOC 数据库系统笔记(一):初步认识数据库系统
    PTA A1014
    MOOC C++笔记(四):运算符重载
    PTA A1013
    PTA A1011&A1012
    1.1.22 同样的文档,行数不一样
  • 原文地址:https://www.cnblogs.com/mingle/p/1708317.html
Copyright © 2011-2022 走看看