Нет описания

Validate.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using System.Text;
  2. using System.Text.RegularExpressions;
  3. namespace CallCenter.Utility
  4. {
  5. public class Validate
  6. {
  7. private static Regex RegNumber = new Regex("^[0-9]+$");
  8. private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
  9. private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
  10. private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
  11. private static Regex RegEmail = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
  12. private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
  13. #region 用户名密码格式
  14. /// <summary>
  15. /// 返回字符串真实长度, 1个汉字长度为2
  16. /// </summary>
  17. /// <returns>字符长度</returns>
  18. public static int GetStringLength(string stringValue)
  19. {
  20. return Encoding.Default.GetBytes(stringValue).Length;
  21. }
  22. /// <summary>
  23. /// 检测用户名格式是否有效
  24. /// </summary>
  25. /// <param name="userName"></param>
  26. /// <returns></returns>
  27. public static bool IsValidUserName(string userName)
  28. {
  29. int userNameLength = GetStringLength(userName);
  30. if (userNameLength >= 4 && userNameLength <= 20 && Regex.IsMatch(userName, @"^([\u4e00-\u9fa5A-Za-z_0-9]{0,})$"))
  31. { // 判断用户名的长度(4-20个字符)及内容(只能是汉字、字母、下划线、数字)是否合法
  32. return true;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. /// <summary>
  40. /// 密码有效性
  41. /// </summary>
  42. /// <param name="password"></param>
  43. /// <returns></returns>
  44. public static bool IsValidPassword(string password)
  45. {
  46. return Regex.IsMatch(password, @"^[A-Za-z_0-9]{6,16}$");
  47. }
  48. #endregion
  49. #region 数字字符串检查
  50. /// <summary>
  51. /// int有效性
  52. /// </summary>
  53. /// <param name="val"></param>
  54. /// <returns></returns>
  55. static public bool IsValidInt(string val)
  56. {
  57. return Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
  58. }
  59. /// <summary>
  60. /// 是否数字字符串
  61. /// </summary>
  62. /// <param name="inputData">输入字符串</param>
  63. /// <returns></returns>
  64. public static bool IsNumber(string inputData)
  65. {
  66. Match m = RegNumber.Match(inputData);
  67. return m.Success;
  68. }
  69. /// <summary>
  70. /// 是否数字字符串 可带正负号
  71. /// </summary>
  72. /// <param name="inputData">输入字符串</param>
  73. /// <returns></returns>
  74. public static bool IsNumberSign(string inputData)
  75. {
  76. Match m = RegNumberSign.Match(inputData);
  77. return m.Success;
  78. }
  79. /// <summary>
  80. /// 是否是浮点数
  81. /// </summary>
  82. /// <param name="inputData">输入字符串</param>
  83. /// <returns></returns>
  84. public static bool IsDecimal(string inputData)
  85. {
  86. Match m = RegDecimal.Match(inputData);
  87. return m.Success;
  88. }
  89. /// <summary>
  90. /// 是否是浮点数 可带正负号
  91. /// </summary>
  92. /// <param name="inputData">输入字符串</param>
  93. /// <returns></returns>
  94. public static bool IsDecimalSign(string inputData)
  95. {
  96. Match m = RegDecimalSign.Match(inputData);
  97. return m.Success;
  98. }
  99. #endregion
  100. #region 中文检测
  101. /// <summary>
  102. /// 检测是否有中文字符
  103. /// </summary>
  104. /// <param name="inputData"></param>
  105. /// <returns></returns>
  106. public static bool IsHasCHZN(string inputData)
  107. {
  108. Match m = RegCHZN.Match(inputData);
  109. return m.Success;
  110. }
  111. /// <summary>
  112. /// 检测含有中文字符串的实际长度
  113. /// </summary>
  114. /// <param name="str">字符串</param>
  115. public static int GetCHZNLength(string inputData)
  116. {
  117. System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
  118. byte[] bytes = n.GetBytes(inputData);
  119. int length = 0; // l 为字符串之实际长度
  120. for (int i = 0; i <= bytes.Length - 1; i++)
  121. {
  122. if (bytes[i] == 63) //判断是否为汉字或全脚符号
  123. {
  124. length++;
  125. }
  126. length++;
  127. }
  128. return length;
  129. }
  130. #endregion
  131. #region 常用格式
  132. /// <summary>
  133. /// 验证身份证是否合法 15 和 18位两种
  134. /// </summary>
  135. /// <param name="idCard">要验证的身份证</param>
  136. public static bool IsIdCard(string idCard)
  137. {
  138. if (string.IsNullOrEmpty(idCard))
  139. {
  140. return false;
  141. }
  142. if (idCard.Length == 15)
  143. {
  144. return Regex.IsMatch(idCard, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
  145. }
  146. else if (idCard.Length == 18)
  147. {
  148. return Regex.IsMatch(idCard, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$", RegexOptions.IgnoreCase);
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. }
  155. /// <summary>
  156. /// 是否是邮件地址
  157. /// </summary>
  158. /// <param name="inputData">输入字符串</param>
  159. /// <returns></returns>
  160. public static bool IsEmail(string inputData)
  161. {
  162. Match m = RegEmail.Match(inputData);
  163. return m.Success;
  164. }
  165. /// <summary>
  166. /// 邮编有效性
  167. /// </summary>
  168. /// <param name="zip"></param>
  169. /// <returns></returns>
  170. public static bool IsValidZip(string zip)
  171. {
  172. Regex rx = new Regex(@"^\d{6}$", RegexOptions.None);
  173. Match m = rx.Match(zip);
  174. return m.Success;
  175. }
  176. /// <summary>
  177. /// 固定电话有效性
  178. /// </summary>
  179. /// <param name="phone"></param>
  180. /// <returns></returns>
  181. public static bool IsValidPhone(string phone)
  182. {
  183. Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$", RegexOptions.None);
  184. Match m = rx.Match(phone);
  185. return m.Success;
  186. }
  187. /// <summary>
  188. /// 手机有效性
  189. /// </summary>
  190. /// <param name="strMobile"></param>
  191. /// <returns></returns>
  192. public static bool IsValidMobile(string mobile)
  193. {
  194. //Regex rx = new Regex(@"^(13|15|17|18|19)\d{9}$", RegexOptions.None);
  195. Regex rx = new Regex(@"^(13|14|15|16|17|18|19)\d{9}$", RegexOptions.None);
  196. Match m = rx.Match(mobile);
  197. return m.Success;
  198. }
  199. /// <summary>
  200. /// 电话有效性(固话和手机 )
  201. /// </summary>
  202. /// <param name="strVla"></param>
  203. /// <returns></returns>
  204. public static bool IsValidPhoneAndMobile(string number)
  205. {
  206. Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$|^(13|15)\d{9}$", RegexOptions.None);
  207. Match m = rx.Match(number);
  208. return m.Success;
  209. }
  210. /// <summary>
  211. /// Url有效性
  212. /// </summary>
  213. /// <param name="url"></param>
  214. /// <returns></returns>
  215. static public bool IsValidURL(string url)
  216. {
  217. return Regex.IsMatch(url, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$");
  218. }
  219. /// <summary>
  220. /// IP有效性
  221. /// </summary>
  222. /// <param name="ip"></param>
  223. /// <returns></returns>
  224. public static bool IsValidIP(string ip)
  225. {
  226. return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
  227. }
  228. /// <summary>
  229. /// domain 有效性
  230. /// </summary>
  231. /// <param name="host">域名</param>
  232. /// <returns></returns>
  233. public static bool IsValidDomain(string host)
  234. {
  235. Regex r = new Regex(@"^\d+$");
  236. if (host.IndexOf(".") == -1)
  237. {
  238. return false;
  239. }
  240. return r.IsMatch(host.Replace(".", string.Empty)) ? false : true;
  241. }
  242. /// <summary>
  243. /// 判断是否为base64字符串
  244. /// </summary>
  245. /// <param name="str"></param>
  246. /// <returns></returns>
  247. public static bool IsBase64String(string str)
  248. {
  249. return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
  250. }
  251. /// <summary>
  252. /// 验证字符串是否是GUID
  253. /// </summary>
  254. /// <param name="guid">字符串</param>
  255. /// <returns></returns>
  256. public static bool IsGuid(string guid)
  257. {
  258. if (string.IsNullOrEmpty(guid))
  259. return false;
  260. return Regex.IsMatch(guid, "[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase);
  261. }
  262. #endregion
  263. #region 日期检查
  264. /// <summary>
  265. /// 判断输入的字符是否为日期
  266. /// </summary>
  267. /// <param name="strValue"></param>
  268. /// <returns></returns>
  269. public static bool IsDate(string strValue)
  270. {
  271. return Regex.IsMatch(strValue, @"^((\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]))))))");
  272. }
  273. /// <summary>
  274. /// 判断输入的字符是否为日期,如2004-07-12 14:25|||1900-01-01 00:00|||9999-12-31 23:59
  275. /// </summary>
  276. /// <param name="strValue"></param>
  277. /// <returns></returns>
  278. public static bool IsDateHourMinute(string strValue)
  279. {
  280. return Regex.IsMatch(strValue, @"^(19[0-9]{2}|[2-9][0-9]{3})-((0(1|3|5|7|8)|10|12)-(0[1-9]|1[0-9]|2[0-9]|3[0-1])|(0(4|6|9)|11)-(0[1-9]|1[0-9]|2[0-9]|30)|(02)-(0[1-9]|1[0-9]|2[0-9]))\x20(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){1}$");
  281. }
  282. #endregion
  283. #region 其他
  284. /// <summary>
  285. /// 检查字符串最大长度,返回指定长度的串
  286. /// </summary>
  287. /// <param name="sqlInput">输入字符串</param>
  288. /// <param name="maxLength">最大长度</param>
  289. /// <returns></returns>
  290. public static string CheckMathLength(string inputData, int maxLength)
  291. {
  292. if (inputData != null && inputData != string.Empty)
  293. {
  294. inputData = inputData.Trim();
  295. if (inputData.Length > maxLength)//按最大长度截取字符串
  296. {
  297. inputData = inputData.Substring(0, maxLength);
  298. }
  299. }
  300. return inputData;
  301. }
  302. /// <summary>
  303. /// 转换成 HTML code
  304. /// </summary>
  305. /// <param name="str">string</param>
  306. /// <returns>string</returns>
  307. public static string Encode(string str)
  308. {
  309. str = str.Replace("&", "&amp;");
  310. str = str.Replace("'", "''");
  311. str = str.Replace("\"", "&quot;");
  312. str = str.Replace(" ", "&nbsp;");
  313. str = str.Replace("<", "&lt;");
  314. str = str.Replace(">", "&gt;");
  315. str = str.Replace("\n", "<br>");
  316. return str;
  317. }
  318. /// <summary>
  319. ///解析html成 普通文本
  320. /// </summary>
  321. /// <param name="str">string</param>
  322. /// <returns>string</returns>
  323. public static string Decode(string str)
  324. {
  325. str = str.Replace("<br>", "\n");
  326. str = str.Replace("&gt;", ">");
  327. str = str.Replace("&lt;", "<");
  328. str = str.Replace("&nbsp;", " ");
  329. str = str.Replace("&quot;", "\"");
  330. return str;
  331. }
  332. #endregion
  333. }
  334. }