架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 22866|回复: 0

[资料] c#各种输入格式验证

[复制链接]
发表于 2016-4-12 23:06:22 | 显示全部楼层 |阅读模式

  1. /// <summary>
  2.     /// 各种输入格式验证
  3.     /// </summary>
  4.     public class ValidateUtil
  5.     {
  6.         private static Regex RegNumber = new Regex("^[0-9]+$");
  7.         private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
  8.         private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
  9.         private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
  10.         private static Regex RegEmail = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
  11.         private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");

  12.         #region 用户名密码格式

  13.         /// <summary>
  14.         /// 返回字符串真实长度, 1个汉字长度为2
  15.         /// </summary>
  16.         /// <returns>字符长度</returns>
  17.         public static int GetStringLength(string stringValue)
  18.         {
  19.             return Encoding.Default.GetBytes(stringValue).Length;
  20.         }

  21.         /// <summary>
  22.         /// 检测用户名格式是否有效
  23.         /// </summary>
  24.         /// <param name="userName"></param>
  25.         /// <returns></returns>
  26.         public static bool IsValidUserName(string userName)
  27.         {
  28.             int userNameLength = GetStringLength(userName);
  29.             if (userNameLength >= 4 && userNameLength <= 20 && Regex.IsMatch(userName, @"^([\u4e00-\u9fa5A-Za-z_0-9]{0,})$"))
  30.             {   // 判断用户名的长度(4-20个字符)及内容(只能是汉字、字母、下划线、数字)是否合法
  31.                 return true;
  32.             }
  33.             else
  34.             {
  35.                 return false;
  36.             }
  37.         }

  38.         /// <summary>
  39.         /// 密码有效性
  40.         /// </summary>
  41.         /// <param name="password"></param>
  42.         /// <returns></returns>
  43.         public static bool IsValidPassword(string password)
  44.         {
  45.             return Regex.IsMatch(password, @"^[A-Za-z_0-9]{6,16}$");
  46.         }
  47.         #endregion

  48.         #region 数字字符串检查

  49.         /// <summary>
  50.         /// int有效性
  51.         /// </summary>
  52.         /// <param name="val"></param>
  53.         /// <returns></returns>
  54.         static public bool IsValidInt(string val)
  55.         {
  56.             return Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
  57.         }
  58.         static public bool IsValidAccountName(string name)
  59.         {
  60.             return Regex.IsMatch(name, @"[^\u4E00-\u9FA5]*");
  61.         }
  62.         /// <summary>
  63.         /// 简单银行卡账号检查
  64.         /// </summary>
  65.         /// <param name="number"></param>
  66.         /// <returns></returns>
  67.         static public bool IsValidAccountNumber(string number)
  68.         {
  69.             return Regex.IsMatch(number, @"/\D/g{1}|/\D/g{3}");
  70.         }
  71.         /// <summary>
  72.         /// 是否数字字符串
  73.         /// </summary>
  74.         /// <param name="inputData">输入字符串</param>
  75.         /// <returns></returns>
  76.         public static bool IsNumber(string inputData)
  77.         {
  78.             Match m = RegNumber.Match(inputData);
  79.             return m.Success;
  80.         }

  81.         /// <summary>
  82.         /// 是否数字字符串 可带正负号
  83.         /// </summary>
  84.         /// <param name="inputData">输入字符串</param>
  85.         /// <returns></returns>
  86.         public static bool IsNumberSign(string inputData)
  87.         {
  88.             Match m = RegNumberSign.Match(inputData);
  89.             return m.Success;
  90.         }

  91.         /// <summary>
  92.         /// 是否是浮点数
  93.         /// </summary>
  94.         /// <param name="inputData">输入字符串</param>
  95.         /// <returns></returns>
  96.         public static bool IsDecimal(string inputData)
  97.         {
  98.             Match m = RegDecimal.Match(inputData);
  99.             return m.Success;
  100.         }

  101.         /// <summary>
  102.         /// 是否是浮点数 可带正负号
  103.         /// </summary>
  104.         /// <param name="inputData">输入字符串</param>
  105.         /// <returns></returns>
  106.         public static bool IsDecimalSign(string inputData)
  107.         {
  108.             Match m = RegDecimalSign.Match(inputData);
  109.             return m.Success;
  110.         }

  111.         #endregion

  112.         #region 中文检测

  113.         /// <summary>
  114.         /// 检测是否有中文字符
  115.         /// </summary>
  116.         /// <param name="inputData"></param>
  117.         /// <returns></returns>
  118.         public static bool IsHasCHZN(string inputData)
  119.         {
  120.             Match m = RegCHZN.Match(inputData);
  121.             return m.Success;
  122.         }

  123.         /// <summary>
  124.         /// 检测含有中文字符串的实际长度
  125.         /// </summary>
  126.         /// <param name="str">字符串</param>
  127.         public static int GetCHZNLength(string inputData)
  128.         {
  129.             System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
  130.             byte[] bytes = n.GetBytes(inputData);

  131.             int length = 0; // l 为字符串之实际长度
  132.             for (int i = 0; i <= bytes.Length - 1; i++)
  133.             {
  134.                 if (bytes[i] == 63) //判断是否为汉字或全脚符号
  135.                 {
  136.                     length++;
  137.                 }
  138.                 length++;
  139.             }
  140.             return length;

  141.         }

  142.         #endregion

  143.         #region 常用格式

  144.         /// <summary>
  145.         /// 验证身份证是否合法  15 和  18位两种
  146.         /// </summary>
  147.         /// <param name="idCard">要验证的身份证</param>
  148.         public static bool IsIdCard(string idCard)
  149.         {
  150.             if (string.IsNullOrEmpty(idCard))
  151.             {
  152.                 return false;
  153.             }

  154.             if (idCard.Length == 15)
  155.             {
  156.                 return Regex.IsMatch(idCard, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
  157.             }
  158.             else if (idCard.Length == 18)
  159.             {
  160.                 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);
  161.             }
  162.             else
  163.             {
  164.                 return false;
  165.             }
  166.         }
  167.         /// <summary>
  168.         /// 验证年龄
  169.         /// </summary>
  170.         /// <param name="age"></param>
  171.         /// <returns></returns>
  172.         public static bool IsAge(string age)
  173.         {
  174.             if (string.IsNullOrEmpty(age))
  175.             {
  176.                 return false;
  177.             }

  178.             if (age.Length >=0 )
  179.             {
  180.                 return Regex.IsMatch(age, @"^^[0-9]*$");
  181.             }
  182.             else
  183.             {
  184.                 return false;
  185.             }
  186.         }   

  187.         /// <summary>
  188.         /// 是否是邮件地址
  189.         /// </summary>
  190.         /// <param name="inputData">输入字符串</param>
  191.         /// <returns></returns>
  192.         public static bool IsEmail(string inputData)
  193.         {
  194.             Match m = RegEmail.Match(inputData);
  195.             return m.Success;
  196.         }

  197.         /// <summary>
  198.         /// 邮编有效性
  199.         /// </summary>
  200.         /// <param name="zip"></param>
  201.         /// <returns></returns>
  202.         public static bool IsValidZip(string zip)
  203.         {
  204.             Regex rx = new Regex(@"^\d{6}$", RegexOptions.None);
  205.             Match m = rx.Match(zip);
  206.             return m.Success;
  207.         }

  208.         /// <summary>
  209.         /// 固定电话有效性
  210.         /// </summary>
  211.         /// <param name="phone"></param>
  212.         /// <returns></returns>
  213.         public static bool IsValidPhone(string phone)
  214.         {
  215.             Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$", RegexOptions.None);
  216.             Match m = rx.Match(phone);
  217.             return m.Success;
  218.         }

  219.         /// <summary>
  220.         /// 手机有效性
  221.         /// </summary>
  222.         /// <param name="strMobile"></param>
  223.         /// <returns></returns>
  224.         public static bool IsValidMobile(string mobile)
  225.         {
  226.             Regex rx = new Regex(@"^(13|15|18)\d{9}$", RegexOptions.None);
  227.             Match m = rx.Match(mobile);
  228.             return m.Success;
  229.         }

  230.         /// <summary>
  231.         /// 电话有效性(固话和手机 )
  232.         /// </summary>
  233.         /// <param name="strVla"></param>
  234.         /// <returns></returns>
  235.         public static bool IsValidPhoneAndMobile(string number)
  236.         {
  237.             Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$|^(13|15)\d{9}$", RegexOptions.None);
  238.             Match m = rx.Match(number);
  239.             return m.Success;
  240.         }

  241.         /// <summary>
  242.         /// Url有效性
  243.         /// </summary>
  244.         /// <param name="url"></param>
  245.         /// <returns></returns>
  246.         static public bool IsValidURL(string url)
  247.         {
  248.             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]$");
  249.         }

  250.         /// <summary>
  251.         /// IP有效性
  252.         /// </summary>
  253.         /// <param name="ip"></param>
  254.         /// <returns></returns>
  255.         public static bool IsValidIP(string ip)
  256.         {
  257.             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?)$");
  258.         }

  259.         /// <summary>
  260.         /// domain 有效性
  261.         /// </summary>
  262.         /// <param name="host">域名</param>
  263.         /// <returns></returns>
  264.         public static bool IsValidDomain(string host)
  265.         {
  266.             Regex r = new Regex(@"^\d+$");
  267.             if (host.IndexOf(".") == -1)
  268.             {
  269.                 return false;
  270.             }
  271.             return r.IsMatch(host.Replace(".", string.Empty)) ? false : true;
  272.         }



  273.         #endregion
  274.         #region 日期检查

  275.         /// <summary>
  276.         /// 判断输入的字符是否为日期
  277.         /// </summary>
  278.         /// <param name="strValue"></param>
  279.         /// <returns></returns>
  280.         public static bool IsDate(string strValue)
  281.         {
  282.             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]))))))");
  283.         }

  284.         /// <summary>
  285.         /// 判断输入的字符是否为日期,如2004-07-12 14:25|||1900-01-01 00:00|||9999-12-31 23:59
  286.         /// </summary>
  287.         /// <param name="strValue"></param>
  288.         /// <returns></returns>
  289.         public static bool IsDateHourMinute(string strValue)
  290.         {
  291.             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}$");
  292.         }

  293.         #endregion

  294.         #region 其他

  295.         /// <summary>
  296.         /// 检查字符串最大长度,返回指定长度的串
  297.         /// </summary>
  298.         /// <param name="sqlInput">输入字符串</param>
  299.         /// <param name="maxLength">最大长度</param>
  300.         /// <returns></returns>                       
  301.         public static string CheckMathLength(string inputData, int maxLength)
  302.         {
  303.             if (inputData != null && inputData != string.Empty)
  304.             {
  305.                 inputData = inputData.Trim();
  306.                 if (inputData.Length > maxLength)//按最大长度截取字符串
  307.                 {
  308.                     inputData = inputData.Substring(0, maxLength);
  309.                 }
  310.             }
  311.             return inputData;
  312.         }

  313.         /// <summary>
  314.         /// 转换成 HTML code
  315.         /// </summary>
  316.         /// <param name="str">string</param>
  317.         /// <returns>string</returns>
  318.         public static string Encode(string str)
  319.         {
  320.             str = str.Replace("&", "&");
  321.             str = str.Replace("'", "''");
  322.             str = str.Replace(""", """);
  323.             str = str.Replace(" ", " ");
  324.             str = str.Replace("<", "<");
  325.             str = str.Replace(">", ">");
  326.             str = str.Replace("\n", "<br>");
  327.             return str;
  328.         }
  329.         /// <summary>
  330.         ///解析html成 普通文本
  331.         /// </summary>
  332.         /// <param name="str">string</param>
  333.         /// <returns>string</returns>
  334.         public static string Decode(string str)
  335.         {
  336.             str = str.Replace("<br>", "\n");
  337.             str = str.Replace(">", ">");
  338.             str = str.Replace("<", "<");
  339.             str = str.Replace(" ", " ");
  340.             str = str.Replace(""", """);
  341.             return str;
  342.         }

  343.         #endregion
  344.     }
复制代码





上一篇:jQuery修改input输入框type属性时报错的处理
下一篇:解决【Unable to make the session state request to the session state server】
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-5-6 09:11

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表