Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Help with Credit Card Expiry Date Validation

$
0
0
Hi, I'm new to Fluent Validation. I know I should be able to do this, and have looked up lots of other posts but just can't figure it out.
ExpireMonth and ExpireYear are strings
This bit works fine
RuleFor(x => x.ExpireMonth).NotEmpty().WithMessage("Blah Blah"));
RuleFor(x => x.ExpireYear).NotEmpty().WithMessage("Blahh"));

Now if the Expiry Month & Year are not empty I want to make sure that the Expiry date is still current. So the logic is
Make sure the year is at least this year
If the year is this year, make sure the month is not less than the current month
I know with the method I first came up with below I get the
"Property name could not be automatically determined for expression x => Convert.ToInt32" but I can't figure how to fix the rule below and also make sure it only runs if the values are not Empty

RuleFor(x => Convert.ToInt32(x.ExpireYear))
.GreaterThanOrEqualTo(DateTime.Now.Year)
.WithMessage("The credit card expiry date is invalid. Please check the values you have entered");
RuleFor(x => onvert.ToInt32(x.ExpireMonth))
 .GreaterThanOrEqualTo(DateTime.Now.Month)
 .When(x => Convert.ToInt32(x.ExpireYear) == DateTime.Now.Year)
 .WithMessage("The credit card expiry date is invalid. Please check the values you have entered");
Any help appreciated

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>