后台正则表达式验证部分部分总结
1 /// <summary> 2 /// 手机号码验证 3 /// </summary> 4 /// <param name="mobile"></param> 5 /// <returns></returns> 6 public static bool MatchMobile(string mobile) 7 { 8 //^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$ 9 if (mobile.IsMatch(@"^1([358][0-9]|4[579]|66|7[0135678]|18[0-9]|9[89])[0-9]{8}$")) 10 { 11 return true; 12 } 13 return false; 14 } 15 16 /// <summary> 17 /// Url格式匹配 18 /// </summary> 19 /// <param name="nickName"></param> 20 /// <returns></returns> 21 public static bool MatchUrl(string url) 22 { 23 if (url.IsMatch(@"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$")) 24 { 25 return true; 26 } 27 return false; 28 } 29 30 /// <summary> 31 /// 验证数字、字母、下划线 32 /// </summary> 33 /// <param name="value">需验证的字符串</param> 34 /// <param name="minLength">最小长度</param> 35 /// <param name="maxLength">最大长度</param> 36 /// <returns></returns> 37 public static bool MatchAlphanumericUnderline(string value, int minLength, int maxLength) 38 { 39 if (value.IsMatch("^[0-9a-zA-Z_]{" + minLength + "," + maxLength + "}$")) 40 { 41 return true; 42 } 43 return false; 44 } 45 59 /// <summary> 60 /// 验证数字(不固定长度) 61 /// </summary> 62 /// <param name="value"> 待验证的值 </param> 63 /// <param name="minLength"> 最下长度 </param> 64 /// <param name="maxLength"> 最大长度 </param> 65 /// <returns></returns> 66 public static bool MatchNumber(string value, int minLength, int maxLength) 67 { 68 if (value.IsMatch(@"^\d{" + minLength + "," + maxLength + "}$")) 69 { 70 return true; 71 } 72 return false; 73 } 74 /// <summary> 75 /// 验证数字(固定长度) 76 /// </summary> 77 /// <param name="value"> 待验证的值 </param> 78 /// <param name="fixedLength"> 固定长度 </param> 79 /// <returns></returns> 80 public static bool MatchNumber(string value, int fixedLength) 81 { 82 if (value.IsMatch(@"^\d{" + fixedLength + "}$")) 83 { 84 return true; 85 } 86 return false; 87 } 88 /// <summary> 89 /// 验证身份证号码 90 /// </summary> 91 /// <param name="value"></param> 92 /// <returns></returns> 93 public static bool MatchIdCard(string value) 94 { 95 string str = value.ToString(); 96 if (str.IsMatch(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$")) 97 { 98 return true; 99 } 100 return false; 101 }116 /// <summary> 117 /// 验证金额是否满足,decimal(18,2),或是整数; 118 /// </summary> 119 /// <param name="value"></param> 120 /// <returns></returns> 121 public static bool MatchApplyCashAmount(Decimal value) 122 { 123 string str = value.ToString(); 124 if (str.IsMatch(@"^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$")) 125 return true; 126 return false; 127 } 128 129 /// <summary> 130 /// 验证银行卡号 131 /// </summary> 132 /// <param name="account"></param>^(\\d{12}|\\d{18})$ 133 /// <returns></returns>/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/ 134 public static bool MathUserCashAccount(string account) 135 { 136 if (account.IsMatch("^[0-9]{14,19}$")) 137 return true; 138 return false; 139 } 140 public static bool MatchNonnegativeInteger(string str) 141 { 142 if (str.IsMatch("^[0-9]\\d*$"))//@"^[1-9]\d*$" 143 return true; 144 return false; 145 } 146 /// <summary> 147 /// 0 和 正整数 148 /// </summary> 149 /// <param name="value"></param> 150 /// <returns></returns> 151 public static bool MatchZeroAndPositiveInteger(string value) 152 { 153 if (value.IsMatch("^(0|\\+?[1-9][0-9]*)$")) 154 return true; 155 return false; 156 }