ValidatePhoneNumber.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Globalization;
  6. using System.Windows.Controls;
  7. using System.Text.RegularExpressions;
  8. namespace Aitex.Core.UI.ValidationRules
  9. {
  10. /// <summary>
  11. /// Validate phone number.
  12. /// </summary>
  13. public class ValidatePhoneNumber : ValidationRule
  14. {
  15. /// <summary>
  16. /// Implement the rule that validates phone number.
  17. /// </summary>
  18. /// <param name="value"></param>
  19. /// <param name="cultureInfo"></param>
  20. /// <returns></returns>
  21. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  22. {
  23. var phoneNumber = (string)value;
  24. var pattern = @"\d{8,13}";
  25. var match = Regex.Match(phoneNumber, pattern);
  26. if (match.Value.Equals(phoneNumber))
  27. {
  28. return new ValidationResult(true, null);
  29. }
  30. return new ValidationResult(false, "Invalid phone number. Phone number must be 8-13 digits.");
  31. }
  32. }
  33. }