Client side validation on button click before AJAX request

安稳与你 提交于 2019-12-12 08:53:41

问题


I'm using MVC 4 and I want to be able to validate a single row, from a collection of rows in a table, to ensure that the fields are entered correctly and pass validation.

The Attributes are correctly applied on the model, for example:

[Required]
[MaxLength(50)]
[MinLength(5)]
public string Name {get; set;}

Now, on the client side - how do I enable client side validation before calling an AJAX method?

To add more complications - I have a button where you can dynamically add a row to the table, fill out fields and finally hit the update button, which will do a POST via ajax. But before this post, I want to validate that particular row on client side.

how can I do this?

thank you


回答1:


As long as you're implementing the validation helpers in your View, this is actually really simple.

var form = $('#form');
$.validator.unobtrusive.parse(form);
form.validate();

if (form.valid()) {
    .... ajax stuff
}

Call the above before initiating the http request and it will make sure that your form is validated.



来源:https://stackoverflow.com/questions/28854305/client-side-validation-on-button-click-before-ajax-request

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