Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.Net Core=I want add personality rules for UserValidator

For Example;
I have password column in the table. AnyBody while enter system must write password. I want contain password minumum one character such as(/,-,?,+,!). I did such one code;

public UserValidator()
{
    RuleFor(p => p.Password).MinimumLength(10);
    RuleFor(p => p.Password).Must(MustBeCharacter);
}
private bool MustBeCharacter(string arg)
{
    return arg.Contains("."+","+"?"+"!"+"*"+"-"+"+");
}

I take a problem. system want all of them ("."+","+"?"+"!"+"*"+"-"+"+"). but I want minimum one more. How can I do this rule.
Thank you so much

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  • In your current approach, you are concatenating all special characters into a single string and applying Contains() on it. That is the reason, the system expects all special chars should be part of your arg string.

  • You should define all special characters inside an array or a list, then use .Any() to check at least once special car should present in the string arg,

    private bool MustBeCharacter(string arg)
    { 
        var specialChars = ".,?!*-+".ToCharArray();
        return specialChars.Any(x => arg.Contains(x));
    }
    

Try online: .Net Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading