| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace RMYY_CallCenter_Api.Utility
- {
- public class ValidateString
- {
- #region 字符串验证
- #region 判断字符串是否为整型
- /**/
- /// <summary>
- /// 判断字符串是否为整型
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- #endregion
- public static bool IsInteger(string source)
- {
- if (string.IsNullOrEmpty(source))
- {
- return false;
- }
- if (Regex.IsMatch(source, "^((\\+)\\d)?\\d*$"))
- {
- return true;
- }
- return false;
- }
- public static bool IsZInteger(string source)
- {
- if (string.IsNullOrEmpty(source))
- {
- return false;
- }
- if (Regex.IsMatch(source, "^[1-9][0-9]*$"))
- {
- return true;
- }
- return false;
- }
- #region 判断字符串是否为整型
- /**/
- /// <summary>
- /// 判断字符串是否为整型
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- #endregion
- public static bool IsDecimal(string source)
- {
- return IsMoney(source);
- }
- #region Email 格式是否合法
- /**/
- /// <summary>
- /// Email 格式是否合法
- /// </summary>
- /// <param name="strEmail"></param>
- #endregion
- public static bool IsEmail(string strEmail)
- {
- return Regex.IsMatch(strEmail, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
- }
- #region 判断一个字符串是否为网址
- /**/
- /// <summary>
- /// 判断一个字符串是否为网址
- /// </summary>
- /// <param name="_value"></param>
- /// <returns></returns>
- #endregion
- public static bool IsUrl(string _value)
- {
- var regex = new Regex(@"(http://)?([\w-]+\.)*[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase);
- return regex.Match(_value).Success;
- }
- #region 判断是否IP
- /**/
- /// <summary>
- /// 判断是否IP
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- #endregion
- public static bool IsIP(string source)
- {
- return Regex.IsMatch(source, @"^(((25[0-5]|2[0-4][0-9]|19[0-1]|19[3-9]|18[0-9]|17[0-1]|17[3-9]|1[0-6][0-9]|1[1-9]|[2-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|(192\.(25[0-5]|2[0-4][0-9]|16[0-7]|169|1[0-5][0-9]|1[7-9][0-9]|[1-9][0-9]|[0-9]))|(172\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|1[0-5]|3[2-9]|[4-9][0-9]|[0-9])))\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$");
- }
- #region 检查字符串是否为A-Z、0-9及下划线以内的字符
- /**/
- /// <summary>
- /// 检查字符串是否为A-Z、0-9及下划线以内的字符
- /// </summary>
- /// <param name="str">被检查的字符串</param>
- /// <returns>是否有特殊字符</returns>
- #endregion
- public static bool IsLetterOrNumber(string str)
- {
- bool b = Regex.IsMatch(str, "[a-zA-Z0-9_]");
- return b;
- }
- #region 检查字符串是否0-9以内的字符
- /**/
- /// <summary>
- /// 检查字符串是否为0-9以内的字符
- /// </summary>
- /// <param name="str">被检查的字符串</param>
- /// <returns>是否有特殊字符</returns>
- #endregion
- public static bool IsNumberStr(string str)
- {
- bool b = Regex.IsMatch(str, "^[0-9]*$");
- return b;
- }
- #region 返回字符串某个位置的一个字符的类型
- /**/
- /// <summary>
- /// 返回字符串某个位置的几个字符是否为a-zA-Z的字母
- /// </summary>
- /// <param name="str">被检查的字符串</param>
- /// <param name="Site">字符位置</param>
- /// <param name="num">长度</param>
- /// <returns>是否是字母</returns>
- #endregion
- public static bool GetStringType(string str, int Site, int num)
- {
- string a = str.Substring(Site, num);
- bool b = Regex.IsMatch(a, "[a-zA-Z]");
- return b;
- }
- #region 验输入字符串是否含有“/\<>:.?*|$]”特殊字符
- /**/
- /// <summary>
- /// 验输入字符串是否含有“/\:.?*|$]”特殊字符
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- #endregion
- public static bool IsSpecialChar(string source)
- {
- var r = new Regex(@"[/\<>:.?*|$]");
- return r.IsMatch(source);
- }
- #region 是否全为中文/日文/韩文字符
- /**/
- /// <summary>
- /// 是否全为中文/日文/韩文字符
- /// </summary>
- /// <param name="source">源字符串</param>
- /// <returns></returns>
- #endregion
- public static bool IsChineseChar(string source)
- {
- //中文/日文/韩文: [\u4E00-\u9FA5]
- //英文:[a-zA-Z]
- return Regex.IsMatch(source, @"^[\u4E00-\u9FA5]+$");
- }
- #region 是否包含双字节字符(允许有单字节字符)
- /**/
- /// <summary>
- /// 是否包含双字节字符(允许有单字节字符)
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- #endregion
- public static bool IsDoubleChar(string source)
- {
- return Regex.IsMatch(source, @"[^\x00-\xff]");
- }
- #region 是否为日期型字符串
- /**/
- /// <summary>
- /// 是否为日期型字符串
- /// </summary>
- /// <param name="source">日期字符串(2005-6-30)</param>
- /// <returns></returns>
- #endregion
- public static bool IsDate(string source)
- {
- return Regex.IsMatch(source, @"^((((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-))$");
- }
- #region 是否为时间型字符串
- /**/
- /// <summary>
- /// 是否为时间型字符串
- /// </summary>
- /// <param name="source">时间字符串(15:00:00)</param>
- /// <returns></returns>
- #endregion
- public static bool IsTime(string source)
- {
- return Regex.IsMatch(source, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
- }
- #region 是否为日期+时间型字符串
- /**/
- /// <summary>
- /// 是否为日期+时间型字符串
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- #endregion
- public static bool IsDateTime(string source)
- {
- return Regex.IsMatch(source, @"^((((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-)) ((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
- }
- #region 是否为钱币型数据
- /**/
- /// <summary>
- /// 是否为钱币型数据
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- #endregion
- public static bool IsMoney(string source)
- {
- if (source == "")
- {
- return false;
- }
- return Regex.IsMatch(source, @"^(([1-9]\d*)|0)(\.\d{1,2})?$");
- }
- #region 判断是否为空字符串
- /**/
- /// <summary>
- /// 是否为空字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- #endregion
- public static bool IsNull(object obj)
- {
- bool rn = obj == null || Convert.ToString(obj).Length <= 0 || Convert.IsDBNull(obj);
- return rn;
- }
- #endregion
- }
- }
|