Email.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Controls;
  8. namespace InvestTracker.Validation
  9. {
  10. public class Email : ValidationRule
  11. {
  12. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  13. {
  14. string? email = value.ToString();
  15. if (email == null)
  16. {
  17. return new ValidationResult(
  18. false,
  19. "Адрес электронной почты не задан"
  20. );
  21. }
  22. // В почте должен быть знак @ и точка после такого знака
  23. int dogIndex = email.IndexOf("@");
  24. if (dogIndex == -1)
  25. {
  26. return new ValidationResult(
  27. false,
  28. "Адрес почты обязан содержать символ \"@\""
  29. );
  30. }
  31. int dotIndex = email.IndexOf('.', dogIndex);
  32. if (dotIndex == -1)
  33. {
  34. return new ValidationResult(
  35. false,
  36. "После символа \"@\" должен следовать символ \".\""
  37. );
  38. }
  39. return ValidationResult.ValidResult;
  40. }
  41. }
  42. }