Validate prefilled jQuery form (invalidate on first field clear, ASP.NET and unobtrustive)

牧云@^-^@ 提交于 2019-11-30 18:53:42

问题


I have a form that can be filled, saved, loaded and re-edited.

When it is loaded and re-edited, it probably begins its life valid. When a field is valid upon load, I want it to invalidate immediately when it is edited contrary to rules. That's my question in a nutshell, and I suspect this is supported by some configuration option I can't find.


Currently, it works properly (the way desired above) only after: 1) it is edited, 2) then the field loses focus while valid, 3) then is edited it again. So it takes an extra valid loss of focus to visualize errors for a pre-filled form.

If it is empty on load (such as the first visit) it should not show errors.

I'm using the following already in my framework:

  • jQuery.validate (the 'approved' one, and the focus of my question)
  • jQuery unobtrusive validation adapter for ASP.NET MVC 3

I don't currently run any custom code to activate this, since the 'unobtrusive' adapter initializes validation (but I'd be happy to, to fix this). I've tried adding 'form.valid()' on page load, but it doesn't fix the above and in fact breaks more stuff.

Thanks, Shannon


回答1:


I've dug into the validation plug-in, and opened a ticket for what I believe is a defect.

https://github.com/jzaefferer/jquery-validation/issues/146

Shannon




回答2:


You could try validating the form manually each time an input was changed.

$(":input").live("change", function(){
    form.validate();
})

From looking at the documentation it seems that you can't call form.valid() without calling form.validate(); first.




回答3:


This constructor works for me:

$('form').validate({
    onfocusout: function(element) {
       this.element(element);
    }, ...
});


来源:https://stackoverflow.com/questions/6281380/validate-prefilled-jquery-form-invalidate-on-first-field-clear-asp-net-and-uno

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