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