I’m using mvc. So I want to validate user input number is 7 digit.
So I wrote a class.
public class StduentValidator : AbstractValidator<graduandModel>
{
public StduentValidator(ILocalizationService localizationService)
{
RuleFor(x => x.student_id).Equal(7)
.WithMessage(localizationService
.GetResource("Hire.graduand.Fields.student_id.Required"));
}
But it is not working.
How to validate 7 digit numbers?
Since you’re using FluentValidation, you want to use the .Matches validator to perform a regular expression match.
Another option is to do something like this (if student_id is a number):
Or, you could use the GreaterThan and LessThan validators, but the above easier to read. Also note that if a number is something like 0000001 then the above won’t work, you’d have to convert it to a string with 7 digits and use the technique below.
if student_id is a string, then something like this: