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
CodePudding user response:
System want all of them because you are looking for entire string and not one character.
you need array first
private string[] arr = [".",",","?","!","*","-"," "];
private bool MustBeCharacter(string arg)
{
return arr.Any(s => args.Contains(s));
}
CodePudding user response:
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 yourarg
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 stringarg
,private bool MustBeCharacter(string arg) { var specialChars = ".,?!*- ".ToCharArray(); return specialChars.Any(x => arg.Contains(x)); }
Try online: .Net Fiddle