Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int Id {get;set;}
- public string Username { get; set; }
- public string Pwd { get; set; }
- public class ConfirmPassword : IValidatableObject
- {
- [Required]
- public string Password { get; set; }
- [Required(ErrorMessage="Confirm Password field is required.")]
- public string ConfirmPwd { get; set; }
- public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
- {
- string regex1 = @"^.{8,10}$"; // 8 - 10 characters
- Match requirement1 = Regex.Match(Password, regex1);
- if (Password != ConfirmPwd)
- yield return new ValidationResult("Password and Confirm Password is not identical.");
- if (!requirement1.Success)
- yield return new ValidationResult("Password must be between 8 and 10 characters.");
- }
- }
- namespace
- {
- public class User
- {
- public int Id {get;set;}
- public string Username { get; set; }
- public string Pwd { get; set; }
- }
- }
- namespace
- {
- public class ConfirmPassword : IValidatableObject
- {
- [Required]
- public string Password { get; set; }
- [Required(ErrorMessage="Confirm Password field is required.")]
- public string ConfirmPwd { get; set; }
- public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
- {
- string regex1 = @"^.{8,10}$"; // 8 - 10 characters
- string regex2 = @"(?:.*?[A-Z]){1}"; // 1 uppercase
- string regex3 = ""; // 1 lowercase
- string regex4 = ""; // 1 numeric
- Match requirement1 = Regex.Match(Password, regex1);
- Match requirement2 = Regex.Match(Password, regex2);
- Match requirement3 = Regex.Match(Password, regex3);
- Match requirement4 = Regex.Match(Password, regex4);
- if (Password != ConfirmPwd)
- yield return new ValidationResult("Password and Confirm Password is not identical.");
- if (!requirement1.Success)
- yield return new ValidationResult("Password must be between 8 and 10 characters.");
- if (!requirement2.Success)
- yield return new ValidationResult("Password must contain at least 1 uppercase letter.");
- if (!requirement3.Success)
- yield return new ValidationResult("Password must contain at least 1 lowercase letter.");
- if (!requirement4.Success)
- yield return new ValidationResult("Password must contain at least 1 numeric character.");
- }
- }
- }
- public LoginViewModel()
- {
- [Required]
- public string LoginId {get; set;}
- [DataType(DataType.Password)]
- public string Password {get; set;}
- [DataType(DataType.Password)]
- public string ConfirmPassword {get; set;}
- // this validation is not db related and can be done client side...
- public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
- {
- string regex1 = @"^.{8,10}$"; // 8 - 10 characters
- Match requirement1 = Regex.Match(Password, regex1);
- if (Password != ConfirmPwd)
- yield return new ValidationResult("Password and Confirm Password is not identical.");
- if (!requirement1.Success)
- yield return new ValidationResult("Password must be between 8 and 10 characters.");
- }
- }
- [HttpGet]
- public ActionResult Login()
- {
- var model = new LoginViewModel();
- return View("Login",model);
- }
- [HttpPost]
- public ActionResult Login(LoginViewModel model)
- {
- var user = GetUserFromRepositoryByUsername(model.username);
- if(user != null)
- {
- if(user.password == model.Password)
- {
- RedirectToAction("YouLoggedInYay!");
- }
- }
- // if we made it this far, the user didn't exist or the password was wrong.
- // Highlight the username field red and add a validation error message.
- ModelState.AddError("Username","Your credentials were invalid punk!");
- return View("Login",model);
- }
- public class ConfirmPassword
- {
- User model;
- [Required]
- public string Username
- {
- get { return this.model.Username; }
- set { this.model.Username = value; }
- }
- [Required]
- [DataType(DataType.Password)]
- public string Password
- {
- get { return this.model.Pwd; }
- set { this.model.Pwd = value; }
- }
- [Required(ErrorMessage = "Confirm Password field is required.")]
- [Compare("NewPassword",
- ErrorMessage = "The new password and confirmation password do not match.")]
- [RegularExpression(@"^.{8,10}$")]
- [DataType(DataType.Password)]
- public string ConfirmPwd { get; set; }
- public ConfirmPassword()
- {
- this.model = new User();
- }
- public ConfirmPassword(User model)
- {
- this.model = model;
- }
- }
Add Comment
Please, Sign In to add comment