/SubSonic.Core/Extensions/Validation.cs

https://github.com/henrylu/SubSonic-3.0 · C# · 486 lines · 220 code · 35 blank · 231 comment · 17 complexity · b98e0150e51566f61a5acd8ae293d849 MD5 · raw file

  1. //
  2. // SubSonic - http://subsonicproject.com
  3. //
  4. // The contents of this file are subject to the New BSD
  5. // License (the "License"); you may not use this file
  6. // except in compliance with the License. You may obtain a copy of
  7. // the License at http://www.opensource.org/licenses/bsd-license.php
  8. //
  9. // Software distributed under the License is distributed on an
  10. // "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11. // implied. See the License for the specific language governing
  12. // rights and limitations under the License.
  13. //
  14. using System;
  15. using System.Globalization;
  16. using System.Text.RegularExpressions;
  17. namespace SubSonic.Extensions
  18. {
  19. /// <summary>
  20. /// Summary for the Validation class
  21. /// </summary>
  22. public static class Validation
  23. {
  24. /// <summary>
  25. /// Determines whether the specified eval string contains only alpha characters.
  26. /// </summary>
  27. /// <param name="evalString">The eval string.</param>
  28. /// <returns>
  29. /// <c>true</c> if the specified eval string is alpha; otherwise, <c>false</c>.
  30. /// </returns>
  31. public static bool IsAlpha(this string evalString)
  32. {
  33. return !Regex.IsMatch(evalString, RegexPattern.ALPHA);
  34. }
  35. /// <summary>
  36. /// Determines whether the specified eval string contains only alphanumeric characters
  37. /// </summary>
  38. /// <param name="evalString">The eval string.</param>
  39. /// <returns>
  40. /// <c>true</c> if the string is alphanumeric; otherwise, <c>false</c>.
  41. /// </returns>
  42. public static bool IsAlphaNumeric(this string evalString)
  43. {
  44. return !Regex.IsMatch(evalString, RegexPattern.ALPHA_NUMERIC);
  45. }
  46. /// <summary>
  47. /// Determines whether the specified eval string contains only alphanumeric characters
  48. /// </summary>
  49. /// <param name="evalString">The eval string.</param>
  50. /// <param name="allowSpaces">if set to <c>true</c> [allow spaces].</param>
  51. /// <returns>
  52. /// <c>true</c> if the string is alphanumeric; otherwise, <c>false</c>.
  53. /// </returns>
  54. public static bool IsAlphaNumeric(this string evalString, bool allowSpaces)
  55. {
  56. if(allowSpaces)
  57. return !Regex.IsMatch(evalString, RegexPattern.ALPHA_NUMERIC_SPACE);
  58. return IsAlphaNumeric(evalString);
  59. }
  60. /// <summary>
  61. /// Determines whether the specified eval string contains only numeric characters
  62. /// </summary>
  63. /// <param name="evalString">The eval string.</param>
  64. /// <returns>
  65. /// <c>true</c> if the string is numeric; otherwise, <c>false</c>.
  66. /// </returns>
  67. public static bool IsNumeric(this string evalString)
  68. {
  69. return !Regex.IsMatch(evalString, RegexPattern.NUMERIC);
  70. }
  71. /// <summary>
  72. /// Determines whether the specified email address string is valid based on regular expression evaluation.
  73. /// </summary>
  74. /// <param name="emailAddressString">The email address string.</param>
  75. /// <returns>
  76. /// <c>true</c> if the specified email address is valid; otherwise, <c>false</c>.
  77. /// </returns>
  78. public static bool IsEmail(this string emailAddressString)
  79. {
  80. return Regex.IsMatch(emailAddressString, RegexPattern.EMAIL);
  81. }
  82. /// <summary>
  83. /// Determines whether the specified string is lower case.
  84. /// </summary>
  85. /// <param name="inputString">The input string.</param>
  86. /// <returns>
  87. /// <c>true</c> if the specified string is lower case; otherwise, <c>false</c>.
  88. /// </returns>
  89. public static bool IsLowerCase(this string inputString)
  90. {
  91. return Regex.IsMatch(inputString, RegexPattern.LOWER_CASE);
  92. }
  93. /// <summary>
  94. /// Determines whether the specified string is upper case.
  95. /// </summary>
  96. /// <param name="inputString">The input string.</param>
  97. /// <returns>
  98. /// <c>true</c> if the specified string is upper case; otherwise, <c>false</c>.
  99. /// </returns>
  100. public static bool IsUpperCase(this string inputString)
  101. {
  102. return Regex.IsMatch(inputString, RegexPattern.UPPER_CASE);
  103. }
  104. /// <summary>
  105. /// Determines whether the specified string is a valid GUID.
  106. /// </summary>
  107. /// <param name="guid">The GUID.</param>
  108. /// <returns>
  109. /// <c>true</c> if the specified string is a valid GUID; otherwise, <c>false</c>.
  110. /// </returns>
  111. public static bool IsGuid(this string guid)
  112. {
  113. return Regex.IsMatch(guid, RegexPattern.GUID);
  114. }
  115. /// <summary>
  116. /// Determines whether the specified string is a valid US Zip Code, using either 5 or 5+4 format.
  117. /// </summary>
  118. /// <param name="zipCode">The zip code.</param>
  119. /// <returns>
  120. /// <c>true</c> if it is a valid zip code; otherwise, <c>false</c>.
  121. /// </returns>
  122. public static bool IsZIPCodeAny(this string zipCode)
  123. {
  124. return Regex.IsMatch(zipCode, RegexPattern.US_ZIPCODE_PLUS_FOUR_OPTIONAL);
  125. }
  126. /// <summary>
  127. /// Determines whether the specified string is a valid US Zip Code, using the 5 digit format.
  128. /// </summary>
  129. /// <param name="zipCode">The zip code.</param>
  130. /// <returns>
  131. /// <c>true</c> if it is a valid zip code; otherwise, <c>false</c>.
  132. /// </returns>
  133. public static bool IsZIPCodeFive(this string zipCode)
  134. {
  135. return Regex.IsMatch(zipCode, RegexPattern.US_ZIPCODE);
  136. }
  137. /// <summary>
  138. /// Determines whether the specified string is a valid US Zip Code, using the 5+4 format.
  139. /// </summary>
  140. /// <param name="zipCode">The zip code.</param>
  141. /// <returns>
  142. /// <c>true</c> if it is a valid zip code; otherwise, <c>false</c>.
  143. /// </returns>
  144. public static bool IsZIPCodeFivePlusFour(this string zipCode)
  145. {
  146. return Regex.IsMatch(zipCode, RegexPattern.US_ZIPCODE_PLUS_FOUR);
  147. }
  148. /// <summary>
  149. /// Determines whether the specified string is a valid Social Security number. Dashes are optional.
  150. /// </summary>
  151. /// <param name="socialSecurityNumber">The Social Security Number</param>
  152. /// <returns>
  153. /// <c>true</c> if it is a valid Social Security number; otherwise, <c>false</c>.
  154. /// </returns>
  155. public static bool IsSocialSecurityNumber(this string socialSecurityNumber)
  156. {
  157. return Regex.IsMatch(socialSecurityNumber, RegexPattern.SOCIAL_SECURITY);
  158. }
  159. /// <summary>
  160. /// Determines whether the specified string is a valid IP address.
  161. /// </summary>
  162. /// <param name="ipAddress">The ip address.</param>
  163. /// <returns>
  164. /// <c>true</c> if valid; otherwise, <c>false</c>.
  165. /// </returns>
  166. public static bool IsIPAddress(this string ipAddress)
  167. {
  168. return Regex.IsMatch(ipAddress, RegexPattern.IP_ADDRESS);
  169. }
  170. /// <summary>
  171. /// Determines whether the specified string is a valid US phone number using the referenced regex string.
  172. /// </summary>
  173. /// <param name="telephoneNumber">The telephone number.</param>
  174. /// <returns>
  175. /// <c>true</c> if valid; otherwise, <c>false</c>.
  176. /// </returns>
  177. public static bool IsUSTelephoneNumber(this string telephoneNumber)
  178. {
  179. return Regex.IsMatch(telephoneNumber, RegexPattern.US_TELEPHONE);
  180. }
  181. /// <summary>
  182. /// Determines whether the specified string is a valid currency string using the referenced regex string.
  183. /// </summary>
  184. /// <param name="currency">The currency string.</param>
  185. /// <returns>
  186. /// <c>true</c> if valid; otherwise, <c>false</c>.
  187. /// </returns>
  188. public static bool IsUSCurrency(this string currency)
  189. {
  190. return Regex.IsMatch(currency, RegexPattern.US_CURRENCY);
  191. }
  192. /// <summary>
  193. /// Determines whether the specified string is a valid URL string using the referenced regex string.
  194. /// </summary>
  195. /// <param name="url">The URL string.</param>
  196. /// <returns>
  197. /// <c>true</c> if valid; otherwise, <c>false</c>.
  198. /// </returns>
  199. public static bool IsURL(this string url)
  200. {
  201. return Regex.IsMatch(url, RegexPattern.URL);
  202. }
  203. /// <summary>
  204. /// Determines whether the specified string is consider a strong password based on the supplied string.
  205. /// </summary>
  206. /// <param name="password">The password.</param>
  207. /// <returns>
  208. /// <c>true</c> if strong; otherwise, <c>false</c>.
  209. /// </returns>
  210. public static bool IsStrongPassword(this string password)
  211. {
  212. return Regex.IsMatch(password, RegexPattern.STRONG_PASSWORD);
  213. }
  214. #region Credit Cards
  215. /// <summary>
  216. /// Determines whether the specified string is a valid credit, based on matching any one of the eight credit card strings
  217. /// </summary>
  218. /// <param name="creditCard">The credit card.</param>
  219. /// <returns>
  220. /// <c>true</c> if valid; otherwise, <c>false</c>.
  221. /// </returns>
  222. public static bool IsCreditCardAny(this string creditCard)
  223. {
  224. if(CreditPassesFormatCheck(creditCard))
  225. {
  226. creditCard = CleanCreditCardNumber(creditCard);
  227. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_AMERICAN_EXPRESS) ||
  228. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_CARTE_BLANCHE) ||
  229. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_DINERS_CLUB) ||
  230. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_DISCOVER) ||
  231. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_EN_ROUTE) ||
  232. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_JCB) ||
  233. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_MASTER_CARD) ||
  234. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_VISA);
  235. }
  236. return false;
  237. }
  238. /// <summary>
  239. /// Determines whether the specified string is an American Express, Discover, MasterCard, or Visa
  240. /// </summary>
  241. /// <param name="creditCard">The credit card.</param>
  242. /// <returns>
  243. /// <c>true</c> if valid; otherwise, <c>false</c>.
  244. /// </returns>
  245. public static bool IsCreditCardBigFour(this string creditCard)
  246. {
  247. if(CreditPassesFormatCheck(creditCard))
  248. {
  249. creditCard = CleanCreditCardNumber(creditCard);
  250. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_AMERICAN_EXPRESS) ||
  251. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_DISCOVER) ||
  252. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_MASTER_CARD) ||
  253. Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_VISA);
  254. }
  255. return false;
  256. }
  257. /// <summary>
  258. /// Determines whether the specified string is an American Express card
  259. /// </summary>
  260. /// <param name="creditCard">The credit card.</param>
  261. /// <returns>
  262. /// <c>true</c> if valid; otherwise, <c>false</c>.
  263. /// </returns>
  264. public static bool IsCreditCardAmericanExpress(this string creditCard)
  265. {
  266. if(CreditPassesFormatCheck(creditCard))
  267. {
  268. creditCard = CleanCreditCardNumber(creditCard);
  269. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_AMERICAN_EXPRESS);
  270. }
  271. return false;
  272. }
  273. /// <summary>
  274. /// Determines whether the specified string is an Carte Blanche card
  275. /// </summary>
  276. /// <param name="creditCard">The credit card.</param>
  277. /// <returns>
  278. /// <c>true</c> if valid; otherwise, <c>false</c>.
  279. /// </returns>
  280. public static bool IsCreditCardCarteBlanche(this string creditCard)
  281. {
  282. if(CreditPassesFormatCheck(creditCard))
  283. {
  284. creditCard = CleanCreditCardNumber(creditCard);
  285. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_CARTE_BLANCHE);
  286. }
  287. return false;
  288. }
  289. /// <summary>
  290. /// Determines whether the specified string is an Diner's Club card
  291. /// </summary>
  292. /// <param name="creditCard">The credit card.</param>
  293. /// <returns>
  294. /// <c>true</c> if valid; otherwise, <c>false</c>.
  295. /// </returns>
  296. public static bool IsCreditCardDinersClub(this string creditCard)
  297. {
  298. if(CreditPassesFormatCheck(creditCard))
  299. {
  300. creditCard = CleanCreditCardNumber(creditCard);
  301. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_DINERS_CLUB);
  302. }
  303. return false;
  304. }
  305. /// <summary>
  306. /// Determines whether the specified string is a Discover card
  307. /// </summary>
  308. /// <param name="creditCard">The credit card.</param>
  309. /// <returns>
  310. /// <c>true</c> if valid; otherwise, <c>false</c>.
  311. /// </returns>
  312. public static bool IsCreditCardDiscover(this string creditCard)
  313. {
  314. if(CreditPassesFormatCheck(creditCard))
  315. {
  316. creditCard = CleanCreditCardNumber(creditCard);
  317. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_DISCOVER);
  318. }
  319. return false;
  320. }
  321. /// <summary>
  322. /// Determines whether the specified string is an En Route card
  323. /// </summary>
  324. /// <param name="creditCard">The credit card.</param>
  325. /// <returns>
  326. /// <c>true</c> if valid; otherwise, <c>false</c>.
  327. /// </returns>
  328. public static bool IsCreditCardEnRoute(this string creditCard)
  329. {
  330. if(CreditPassesFormatCheck(creditCard))
  331. {
  332. creditCard = CleanCreditCardNumber(creditCard);
  333. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_EN_ROUTE);
  334. }
  335. return false;
  336. }
  337. /// <summary>
  338. /// Determines whether the specified string is an JCB card
  339. /// </summary>
  340. /// <param name="creditCard">The credit card.</param>
  341. /// <returns>
  342. /// <c>true</c> if valid; otherwise, <c>false</c>.
  343. /// </returns>
  344. public static bool IsCreditCardJCB(this string creditCard)
  345. {
  346. if(CreditPassesFormatCheck(creditCard))
  347. {
  348. creditCard = CleanCreditCardNumber(creditCard);
  349. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_JCB);
  350. }
  351. return false;
  352. }
  353. /// <summary>
  354. /// Determines whether the specified string is a Master Card credit card
  355. /// </summary>
  356. /// <param name="creditCard">The credit card.</param>
  357. /// <returns>
  358. /// <c>true</c> if valid; otherwise, <c>false</c>.
  359. /// </returns>
  360. public static bool IsCreditCardMasterCard(this string creditCard)
  361. {
  362. if(CreditPassesFormatCheck(creditCard))
  363. {
  364. creditCard = CleanCreditCardNumber(creditCard);
  365. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_MASTER_CARD);
  366. }
  367. return false;
  368. }
  369. /// <summary>
  370. /// Determines whether the specified string is Visa card.
  371. /// </summary>
  372. /// <param name="creditCard">The credit card.</param>
  373. /// <returns>
  374. /// <c>true</c> if valid; otherwise, <c>false</c>.
  375. /// </returns>
  376. public static bool IsCreditCardVisa(this string creditCard)
  377. {
  378. if(CreditPassesFormatCheck(creditCard))
  379. {
  380. creditCard = CleanCreditCardNumber(creditCard);
  381. return Regex.IsMatch(creditCard, RegexPattern.CREDIT_CARD_VISA);
  382. }
  383. return false;
  384. }
  385. /// <summary>
  386. /// Cleans the credit card number, returning just the numeric values.
  387. /// </summary>
  388. /// <param name="creditCard">The credit card.</param>
  389. /// <returns></returns>
  390. public static string CleanCreditCardNumber(this string creditCard)
  391. {
  392. Regex regex = new Regex(RegexPattern.CREDIT_CARD_STRIP_NON_NUMERIC, RegexOptions.IgnoreCase | RegexOptions.Singleline);
  393. return regex.Replace(creditCard, String.Empty);
  394. }
  395. /// <summary>
  396. /// Determines whether the credit card number, once cleaned, passes the Luhn algorith.
  397. /// See: http://en.wikipedia.org/wiki/Luhn_algorithm
  398. /// </summary>
  399. /// <param name="creditCardNumber">The credit card number.</param>
  400. /// <returns></returns>
  401. private static bool CreditPassesFormatCheck(this string creditCardNumber)
  402. {
  403. creditCardNumber = CleanCreditCardNumber(creditCardNumber);
  404. if(creditCardNumber.IsInteger())
  405. {
  406. int[] numArray = new int[creditCardNumber.Length];
  407. for(int i = 0; i < numArray.Length; i++)
  408. numArray[i] = Convert.ToInt16(creditCardNumber[i].ToString());
  409. return IsValidLuhn(numArray);
  410. }
  411. return false;
  412. }
  413. /// <summary>
  414. /// Determines whether the specified int array passes the Luhn algorith
  415. /// </summary>
  416. /// <param name="digits">The int array to evaluate</param>
  417. /// <returns>
  418. /// <c>true</c> if it validates; otherwise, <c>false</c>.
  419. /// </returns>
  420. public static bool IsValidLuhn(this int[] digits)
  421. {
  422. int sum = 0;
  423. bool alt = false;
  424. for(int i = digits.Length - 1; i >= 0; i--)
  425. {
  426. if(alt)
  427. {
  428. digits[i] *= 2;
  429. if(digits[i] > 9)
  430. digits[i] -= 9; // equivalent to adding the value of digits
  431. }
  432. sum += digits[i];
  433. alt = !alt;
  434. }
  435. return sum % 10 == 0;
  436. }
  437. /// <summary>
  438. /// Determine whether the passed string is numeric, by attempting to parse it to a double
  439. /// </summary>
  440. /// <param name="str">The string to evaluated for numeric conversion</param>
  441. /// <returns>
  442. /// <c>true</c> if the string can be converted to a number; otherwise, <c>false</c>.
  443. /// </returns>
  444. public static bool IsStringNumeric(this string str)
  445. {
  446. double result;
  447. return (double.TryParse(str, NumberStyles.Float, NumberFormatInfo.CurrentInfo, out result));
  448. }
  449. #endregion
  450. }
  451. }