comparison delete me/SilverlightValidation/Validators/UserModelValidator.cs @ 59:3591c26bd63e

MVVMLight added
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sat, 21 Apr 2012 19:20:28 +0100
parents
children
comparison
equal deleted inserted replaced
58:241e2f22ed3c 59:3591c26bd63e
1 using System;
2 using FluentValidation;
3 using SilverlightValidation.Interfaces;
4
5 namespace SilverlightValidation.Validators
6 {
7 public class UserModelValidator : AbstractValidator<IUserModel>
8 {
9 public UserModelValidator()
10 {
11 RuleFor(x => x.Username)
12 .Length(3, 8)
13 .WithMessage("Must be between 3-8 characters.");
14
15 RuleFor(x => x.Password)
16 .Matches(@"^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$")
17 .WithMessage("Must contain lower, upper and numeric chars.");
18
19 RuleFor(x => x.Email)
20 .EmailAddress()
21 .WithMessage("A valid email address is required.");
22
23 RuleFor(x => x.DateOfBirth)
24 .Must(BeAValidDateOfBirth)
25 .WithMessage("Must be within 100 years of today.");
26 }
27
28 private bool BeAValidDateOfBirth(DateTime? dateOfBirth)
29 {
30 if (dateOfBirth == null) return false;
31 if (dateOfBirth.Value > DateTime.Today || dateOfBirth < DateTime.Today.AddYears(-100))
32 return false;
33 return true;
34 }
35 }
36 }