Why does the pattern matching not work in this?

RossalaTTMRossalaTTM Member ✭✭
edited April 4 in Xamarin.Forms

public class PasswordValidator : IValidator
{
const int minLength = 8;
const string pattern = "@.,?!£$%^&*()";
public string Message { get; set; } = $"Password should at least {minLength} characters long and should include a special character {pattern}.";
public bool Check(string value) => !string.IsNullOrEmpty(value) && value.Length >= minLength && value.Contains(pattern);
}

When I run this and type in one of the characters from the pattern plus the 8 char, it does not let me validate it. Have I done something wrong?

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    @RossalaTTM said:
    public class PasswordValidator : IValidator
    {
    const int minLength = 8;
    const string pattern = "@.,?!£$%^&*()";
    public string Message { get; set; } = $"Password should at least {minLength} characters long and should include a special character {pattern}.";
    public bool Check(string value) => !string.IsNullOrEmpty(value) && value.Length >= minLength && value.Contains(pattern);
    }

    When I run this and type in one of the characters from the pattern plus the 8 char, it does not let me validate it. Have I done something wrong?

    This isn't a Xamarin question, it is a basic C# question. Specifically read up on what string.Contains actually does.
    https://docs.microsoft.com/en-us/dotnet/api/system.string.contains?view=netframework-4.7.2

Sign In or Register to comment.