Jquery Validation remote validation not working

早过忘川 提交于 2020-01-03 03:31:11

问题


I am not sure what I am doing wrong. I am using the Jquery.Validate plugin and it has a remote field, so I did this:

$("#mainForm").validate(
    {
        rules:
        {
            UserName:
            {
                required: true
               ,remote: "Test"
            }
         ,messages:
        {
            UserName:
            {
                remote: "UserName has already been chosen. Please choose another one"
            }
        }

     }

The required works fine. It's the remote that has the problem. I am using asp.net MVC and the path is right - it hits my method

public bool Test(string userName)
{
    return false;
}

It returns false according to firebug yet jquery.validate does not kick in. I am using Version 1.5.5 of jquery.validate and jquery 1.3.2

What did I miss?


回答1:


I got caught out by this one also.

You need to return a Json object with true or false, see below:

    public ActionResult IsValidField()
    {
        String the_field = httpContextService.Request["Field_To_Test"];

        if (the_field == another_value)
          return Json(true);
        else 
          return Json(false);
    }


来源:https://stackoverflow.com/questions/1249709/jquery-validation-remote-validation-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!