PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities.Validation/Validation/Rules/IsEmailAddress.cs

#
C# | 88 lines | 44 code | 9 blank | 35 comment | 2 complexity | 4347a7946036102477c25a7b9164888e MD5 | raw file
  1. /*
  2. Copyright (c) 2012 <a href="http://www.gutgames.com">James Craig</a>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.*/
  18. #region Usings
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Reflection;
  22. using System.Text;
  23. using System.Text.RegularExpressions;
  24. using System.Linq;
  25. using Utilities.Validation.BaseClasses;
  26. using Utilities.Validation.Exceptions;
  27. #endregion
  28. namespace Utilities.Validation.Rules
  29. {
  30. /// <summary>
  31. /// Email address
  32. /// </summary>
  33. public class IsEmailAddress<ObjectType> : Rule<ObjectType, string>
  34. {
  35. #region Constructor
  36. /// <summary>
  37. /// Constructor
  38. /// </summary>
  39. /// <param name="ItemToValidate">Item to validate</param>
  40. /// <param name="ErrorMessage">Error message</param>
  41. public IsEmailAddress(Func<ObjectType, string> ItemToValidate, string ErrorMessage)
  42. : base(ItemToValidate, ErrorMessage)
  43. {
  44. }
  45. #endregion
  46. #region Functions
  47. public override void Validate(ObjectType Object)
  48. {
  49. string Value = this.ItemToValidate(Object);
  50. if (string.IsNullOrEmpty(Value))
  51. return;
  52. System.Text.RegularExpressions.Regex TempReg = new System.Text.RegularExpressions.Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
  53. @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
  54. @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
  55. if(!TempReg.IsMatch(Value))
  56. throw new NotValid(ErrorMessage);
  57. }
  58. #endregion
  59. }
  60. /// <summary>
  61. /// IsEmailAddress attribute
  62. /// </summary>
  63. public class IsEmailAddress : BaseAttribute
  64. {
  65. #region Constructor
  66. /// <summary>
  67. /// Constructor
  68. /// </summary>
  69. /// <param name="ErrorMessage">Error message</param>
  70. public IsEmailAddress(string ErrorMessage = "")
  71. : base(ErrorMessage)
  72. {
  73. }
  74. #endregion
  75. }
  76. }