验证在MVC使用JqueryValidation多个文本框 [英] Validating multiple textboxes using JqueryValidation in MVC

查看:79
本文介绍了验证在MVC使用JqueryValidation多个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

渲染荫局部视图两次相同的父view.Since每个子视图有1个文本框,每个我有2个文本框。荫欲以JqueryValidation插件作为

Iam rendering partial a view twice on the same parent view.Since each child view has 1 textbox each I have 2 textboxes. Iam trying to use JqueryValidation Plugin as in

$("#form0").validate({ 
    rules: {
    Address<%=Model.TypeName%>: { 
            required: true, 
            minLength: 8 

        } }, 
    messages: {

    Address<%=Model.TypeName%>: : { 
            required: "Please enter an address", 
            minLength: "Your address must consist of at least 8 characters" 

        } 
    } 

我有两个Model.TypeNames,1)学生; 2)母公司。

I have two Model.TypeNames , 1) student, 2) parent.

当我尝试验证这两个文本框(1学生,1父),只有1号作品之一。第二个多年平均值验证。任何意见/建议,使验证工作两个文本框将是非常美联社preciated。

When I try to validate both the textboxes( 1 for student, 1 for parent) , only the 1st one works. the second one doesnot validate. Any ideas/suggestions to make the validation work for both the textboxes would be highly appreciated.

推荐答案

jQuery的验证通过提供给他们的姓名输入字段规则插件的作品。你需要指定你的规则时提供两个输入。另一种可能性是做到以下几点。包括验证一次:

The jquery validate plugin works by providing rules for input fields given their names. You will need to provide both inputs when specifying your rules. Another possibility is to do the following. Include validation once:

$("#form0").validate({ });

然后添加规则动态地的:

$('input[name=Address<%=Model.TypeName%>]').rules('add', {
    required: true,
    minlength: 8,
    messages: {
        required: 'Please enter an address',
        minlength: 'Your address must consist of at least 8 characters'
    }
});

由于你是其中包括两个谐音,你将最终获得两次调用以.rules(加函数用于输入的字段。

Because you are including two partials you will end up with two calls to the .rules('add' function for both input fields.

这篇关于验证在MVC使用JqueryValidation多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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