ValidationExtension.cs 845 B

123456789101112131415161718192021222324252627282930313233343536
  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. public EmailAttribute()
  14. {
  15. this.ErrorMessage = "邮箱格式不正确";
  16. }
  17. //重写基类方法
  18. public override bool IsValid(object value)
  19. {
  20. if (value == null)
  21. return true;
  22. if (value is string)
  23. {
  24. Regex regEx = new Regex(reg);
  25. return regEx.IsMatch(value.ToString());
  26. }
  27. return false;
  28. }
  29. }
  30. }