PageRenderTime 22ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/SubSonic/Sugar/Validation.cs

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