PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Validator.cs

https://bitbucket.org/lduparc/wp7helpers
C# | 78 lines | 72 code | 6 blank | 0 comment | 2 complexity | 6646137cb48e68fbd14a731ebaf01154 MD5 | raw file
  1. using System;
  2. using System.Windows.Data;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Globalization;
  6. using System.Text.RegularExpressions;
  7. namespace com.wp.helpers {
  8. public class Validators
  9. {
  10. public enum ValidationPatterns
  11. {
  12. email,
  13. icq,
  14. creditcard,
  15. english,
  16. url,
  17. IPv4,
  18. IPv6,
  19. name
  20. }
  21. public static bool Validate(string inputStr, ValidationPatterns pattern)
  22. {
  23. if (!string.IsNullOrEmpty(inputStr))
  24. {
  25. Regex reg = new Regex(GetPatternString(pattern));
  26. return reg.IsMatch(inputStr);
  27. }
  28. else
  29. return false;
  30. }
  31. static string GetPatternString(ValidationPatterns pattern)
  32. {
  33. switch (pattern)
  34. {
  35. case ValidationPatterns.icq:
  36. {
  37. return @"([1-9])+(?:-?\d){4,}";
  38. }
  39. case ValidationPatterns.creditcard:
  40. {
  41. return @"[0-9]{13,16}";
  42. }
  43. case ValidationPatterns.english:
  44. {
  45. return @"^[a-zA-Z0-9]+$";
  46. }
  47. case ValidationPatterns.url:
  48. {
  49. return @"^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$";
  50. }
  51. case ValidationPatterns.IPv4:
  52. {
  53. return @"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)";
  54. }
  55. case ValidationPatterns.IPv6:
  56. {
  57. return @"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)";
  58. }
  59. case ValidationPatterns.name:
  60. {
  61. return @"^[a-zA-Z0-9-_\.]+$";
  62. }
  63. case ValidationPatterns.email:
  64. {
  65. return @"^[a-zA-Z0-9-_\.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$";
  66. }
  67. default:
  68. return null;
  69. }
  70. }
  71. }
  72. }