MobileAttribute.cs 631 B

123456789101112131415161718192021
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Text.RegularExpressions;
  4. namespace GxPress.Common.Validation
  5. {
  6. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
  7. public class MobileAttribute : ValidationAttribute
  8. {
  9. public override bool IsValid(object value)
  10. {
  11. return Regex.IsMatch(value.ToString(), @"^1[3456789]\d{9}$", RegexOptions.IgnoreCase);
  12. }
  13. public override string FormatErrorMessage(string name)
  14. {
  15. return "不是合法的手机号";
  16. }
  17. }
  18. }