EmailAttribute.cs 731 B

123456789101112131415161718192021222324
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace GxPress.Common.Validation
  7. {
  8. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
  9. public class EmailAttribute: ValidationAttribute
  10. {
  11. public override bool IsValid(object value)
  12. {
  13. return Regex.IsMatch(value.ToString(), @"^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$", RegexOptions.IgnoreCase);
  14. }
  15. public override string FormatErrorMessage(string name)
  16. {
  17. return "不是合法的邮箱";
  18. }
  19. }
  20. }