Search code examples
jqueryasp.net-mvcjquery-validateremote-validation

jQuery remote validation not working on keypress, only working on focus change


I have a view model

public class UserRegister
    {
        [Remote("CheckUserEmail")]
        public string Email { get; set; }
    }

There is a Register view which is strongly typed with UserRegister model.

@Html.EditorFor(model => model.Email)

In User controller I have action Register

 public ActionResult Register()
        {
            UserRegister userRegister = new UserRegister();
            //// remote validation does not work on key press when the email field is not empty, 
            //// if i comment the below line or set the email as empty then remote validation works on keypress
            userRegister.Email = "abc@abc.com";
            return View(userRegister);
        }

Remote validation works on keypress event in forms where email is not pre-populated, but when the email field is prepopulated (above code), the remote validation (on keypress event) is not working. This is working only when focus is changed.

Any suggestions please?


Solution

  • i dont know that problem but you can use jquery to validate the from by doing "$("FormSelector").valid()

    so you can do

    $("input[type='text'"]).keypress(function() {
      $(this).valid()
        //Or if you want to validate the whole form
      $(this).closest('form').valid()
    });