Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Here Is my code below I am using Remote validation
- public ActionResult Settings()
- {
- return View();
- }
- //For checking New Password Doesnt match With Old Password
- public JsonResult IsValidUsername(string newPassword)
- {
- AdminRepository service = new AdminRepository();
- return Json(service.IsValidUsername(newPassword), JsonRequestBehavior.AllowGet);
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Settings(AdminViewModels model, FormCollection collection)
- {
- try
- {
- ModelState.Remove("EmailID");
- ModelState.Remove("IsValid");
- if (ModelState.IsValid)
- {
- AdminViewModels adminObj = (AdminViewModels)Session["Admin"];
- model.AdminID = adminObj.AdminID;
- string newPassword = collection["NewPassword"];
- if (!string.IsNullOrEmpty(newPassword))
- {
- bool exists = admin.PasswordUpdate(model, newPassword);
- if (exists)
- ViewBag.message = "Password Updated Successfully";
- else
- ViewBag.messageinvalid = "Invalid Password";
- }
- else
- {
- ViewBag.nullmessage = "Enter New Password";
- }
- }
- return View();
- }
- catch (Exception ex)
- {
- return View("Error", new HandleErrorInfo(ex, "model", "collection"));
- }
- }
- public bool IsValidUsername(string newpassword)
- {
- using (dbHealthSplashEntities dbcontext = new dbHealthSplashEntities())
- {
- return !dbcontext.Admins.Any(user => user.Password == newpassword);
- }
- }
- public class AdminViewModels:IValidatableObject
- {
- public int AdminID { get; set; }
- [Required(ErrorMessage = "EmailID is required")]
- [StringLength(16, ErrorMessage = "Must be between 5 and 50 characters", MinimumLength = 5)]
- [RegularExpression("^[a-zA-Z0-9_\.-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")]
- [EmailAnnotation]
- public string EmailID { get; set; }
- [Required(ErrorMessage = "Password is required")]
- //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
- [DataType(DataType.Password)]
- [Display(Name = "Password")]
- public string Password { get; set; }
- [Required(ErrorMessage = "Enter New Password")]
- //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
- [DataType(DataType.Password)]
- [Display(Name = "newPassword")]
- [Remote("IsValidUsername", "Admin", ErrorMessage = "It Seems You Have Entered Same Password As Old Password!!!")]
- public string newPassword { get; set; }
- [Required]
- [System.ComponentModel.DataAnnotations.Compare("newPassword", ErrorMessage = "The password and confirmation password do not match.")]
- public string ConfirmPassword { get; set; }
- public Nullable<bool> Flag { get; set; }
- public bool RememberMe { get; set; }
- public bool IsValid { get; set; }
- public IEnumerable<ValidationResult> Validate(ValidationContext context)
- {
- if (newPassword == Password)
- yield return new ValidationResult("Passwords should not be the same");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement