Mercurial > silverbladetech
diff SilverlightValidation/SilverlightValidation.PL/Validators/UserModelValidator.cs @ 96:188f8b366e87
Unit test project correctly setup as normal class library DLL.
author | stevenhollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sat, 05 May 2012 13:29:56 +0100 |
parents | SilverlightValidation/SilverlightValidation/Validators/UserModelValidator.cs@241e2f22ed3c |
children | db05a55e3536 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SilverlightValidation/SilverlightValidation.PL/Validators/UserModelValidator.cs Sat May 05 13:29:56 2012 +0100 @@ -0,0 +1,36 @@ +using System; +using FluentValidation; +using SilverlightValidation.Interfaces; + +namespace SilverlightValidation.Validators +{ + public class UserModelValidator : AbstractValidator<IUserModel> + { + public UserModelValidator() + { + RuleFor(x => x.Username) + .Length(3, 8) + .WithMessage("Must be between 3-8 characters."); + + RuleFor(x => x.Password) + .Matches(@"^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$") + .WithMessage("Must contain lower, upper and numeric chars."); + + RuleFor(x => x.Email) + .EmailAddress() + .WithMessage("A valid email address is required."); + + RuleFor(x => x.DateOfBirth) + .Must(BeAValidDateOfBirth) + .WithMessage("Must be within 100 years of today."); + } + + private bool BeAValidDateOfBirth(DateTime? dateOfBirth) + { + if (dateOfBirth == null) return false; + if (dateOfBirth.Value > DateTime.Today || dateOfBirth < DateTime.Today.AddYears(-100)) + return false; + return true; + } + } +}