在AJAX请求之前单击按钮进行客户端验证 [英] Client side validation on button click before AJAX request

查看:98
本文介绍了在AJAX请求之前单击按钮进行客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC 4,并且希望能够从表中的行集合中验证单个行,以确保正确输入字段并通过验证.

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;}

现在,在客户端-如何在调用AJAX方法之前启用客户端验证?

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

要增加更多的复杂性-我有一个按钮,您可以在其中动态向表添加一行,填写字段,最后单击update按钮,这将通过ajax执行POST.但是在发布这篇文章之前,我想在客户端验证该特定行.

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.

我该怎么做?

谢谢

推荐答案

只要您在View中实现验证助手,这实际上就很简单.

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
}

在启动http请求之前,请致电上述内容,这将确保您的表单已通过验证.

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

这篇关于在AJAX请求之前单击按钮进行客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆