ValidationExtension.cs 1022 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Text.RegularExpressions;
  7. using System.Globalization;
  8. namespace EMIS.ViewModel
  9. {
  10. public sealed class EmailAttribute : ValidationAttribute
  11. {
  12. public const string reg = @"^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$";
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public EmailAttribute()
  17. {
  18. this.ErrorMessage = "邮箱格式不正确";
  19. }
  20. /// <summary>
  21. /// 重写基类方法
  22. /// </summary>
  23. /// <param name="value"></param>
  24. /// <returns></returns>
  25. public override bool IsValid(object value)
  26. {
  27. if (value == null)
  28. return true;
  29. if (value is string)
  30. {
  31. Regex regEx = new Regex(reg);
  32. return regEx.IsMatch(value.ToString());
  33. }
  34. return false;
  35. }
  36. }
  37. }