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