jquery validate always returning true

前端 未结 5 1252
北荒
北荒 2020-12-31 21:16

I am using MVC4 w/JQuery and I have a form I created using the @Ajax.BeginForm. It has required fields (set on its view model). I am trying to validate the form before it is

5条回答
  •  梦谈多话
    2020-12-31 22:21

    Question already has an accepted answer, but here's another possible reason for anyone that finds this in the future...

    Make sure that your first validated element in the form has a name attribute, like this:

    
    

    I spent 2 hours tracking down this same issue, and I found that if the FIRST validated element in the form has a name attribute then everything works as you would expect (that is, .valid() will return false if the form is invalid). It seems to make no difference whether the other validated elements have name attributes or not.

    In normal forms, you definitely need name attributes because that's what's used when the data gets submitted to the server. But in more modern environments, such as those using Knockout, there's no reason to have a name attribute on input elements, because the data-binding works to keep your data model updated.

提交回复
热议问题