如何在我的表单中使用 jquery 验证器插件中的条件验证

发布于 2024-12-02 03:31:43 字数 1854 浏览 1 评论 0原文

当我从下拉框中选择类型时,表单字段正在发生变化。 更改意味着隐藏或显示,因此根据下拉值表单字段隐藏和显示,我需要向此表单添加j查询验证插件,

我有一个输入

<div class="form-item">
                                                        <div class="catergory-inputs">
                                                            <input name="address" type="text" class="textarea_medium"
                                                                id="Address" value="<?php echo $address['value'] ; ?>" />
                                                        </div>
                                                    </div>

我验证这个

 var x=$("#contactinfo").validate({
            rules: {

                address: {
                    required: true,
                    minlength: 3,
                    maxlength: 50,
                    lettersonly: true                   
               },
 },

            messages: {

                address: {
                    required: "Enter your Address",
                    minlength: "At least 3 characters long"
                },
            }
        }); 

我想验证这个字段如果只有这个场上并不隐瞒。像这样

 var x=$("#contactinfo").validate({
            rules: {
                if(not hiding) {
                address: {
                    required: true,
                    minlength: 3,
                    maxlength: 50,
                    lettersonly: true
                   } 
               },
 },

            messages: {
                if(not hiding) {
                address: {
                    required: "Enter your Address",
                    minlength: "At least 3 characters long"
                  } 
                },
            }
        }); 

我该怎么做,我在堆栈溢出中看到了类似的问题。但是那里没有 mh 问题的答案,请帮助我...................... …… :(

when i select a type from a drop down box the form fields are changing .
changing means hiding or showing , so according to the drop-down value the form fields are hiding and showing , i need to add j query validation plugin to this form ,

i have a input

<div class="form-item">
                                                        <div class="catergory-inputs">
                                                            <input name="address" type="text" class="textarea_medium"
                                                                id="Address" value="<?php echo $address['value'] ; ?>" />
                                                        </div>
                                                    </div>

i validate this

 var x=$("#contactinfo").validate({
            rules: {

                address: {
                    required: true,
                    minlength: 3,
                    maxlength: 50,
                    lettersonly: true                   
               },
 },

            messages: {

                address: {
                    required: "Enter your Address",
                    minlength: "At least 3 characters long"
                },
            }
        }); 

i want to validate this field if only this field is not hiding . like this

 var x=$("#contactinfo").validate({
            rules: {
                if(not hiding) {
                address: {
                    required: true,
                    minlength: 3,
                    maxlength: 50,
                    lettersonly: true
                   } 
               },
 },

            messages: {
                if(not hiding) {
                address: {
                    required: "Enter your Address",
                    minlength: "At least 3 characters long"
                  } 
                },
            }
        }); 

how can i do this , i saw similar questions in stack-overflow .but there were no answer to mh question there , plz help me ............................. :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萤火眠眠 2024-12-09 03:31:43

我认为你可以使用可见选择器。

http://api.jquery.com/visible-selector/

这是未经测试的,但是;

var x=$("#contactinfo:visible").validate({

顺便说一句,当使用验证插件时,你可以这样做;

<input type=text class="required" id="txtName"/>

那么当您调用 validate() 方法时,如果文本框被隐藏,则不会对其进行验证。诀窍是将“必需”类添加到类属性中

I think you can use the visible selector.

http://api.jquery.com/visible-selector/

this is untested but;

var x=$("#contactinfo:visible").validate({

By the way, when using the validation plugin you can do this;

<input type=text class="required" id="txtName"/>

then when you call the validate() method, if the textbox is hidden it will not be validated. The trick is to add the "required" class to the class attribute

两个我 2024-12-09 03:31:43

我想有条件地验证用户名字段,以确保它不在数据库中,而仅在创建新用户时才进行。因此,在创建新用户时,我放置了一个 javascript 变量 var newUser=true; 否则编辑现有用户 var newUser=false;

然后在验证的规则部分中放置 required: (newUser)?true:false

这会导致 validate 仅当新用户为 true 时才验证用户名字段。如果我们加载现有用户,则只需在 PHP 代码中添加 javascript var,规则的 required 部分就会具有值 false

请记住,您可以将 javascript 放入 json 对象中。因为这都是 javascript 的!

这些是我在学习验证时使用的资源: http://www.ferdychristant.com /blog//articles/DOMM-7LZJN7 //详细介绍如何真正使用 validate 的文章。 http://www.ferdychristant.com/blog//pages/jQuery%20validation %20代码
http://randomactsofcoding.blogspot.com/2008/10/starting-with-jquery-how-to-write.html
http://docs.jquery.com/Plugins/Validation/Methods/required #dependency-expression

http://docs.jquery.com/Plugins/Validation ...您只需点击全部即可!

I wanted to conditionally validate a username field to make sure it was not in the database but only on creating a new user. So when creating a new I placed a javascript variable var newUser=true; else editing existing user var newUser=false;

Then in my rules portion of my validate I put required: (newUser)?true:false

This causes validate to validate the username field only if new user is true. If we loaded up an existing user, then the required portion of the rule will have the value false simply by adding the javascript var in your PHP code.

Remember, you can put javascript into a json object. Because it's all javascript!

These are the resources I used in learning about validate: http://www.ferdychristant.com/blog//articles/DOMM-7LZJN7 //article detailing how to really use validate. http://www.ferdychristant.com/blog//pages/jQuery%20validation%20code
http://randomactsofcoding.blogspot.com/2008/10/starting-with-jquery-how-to-write.html
http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression

There is also an amazing depth of information simply in http://docs.jquery.com/Plugins/Validation ...you just gotta click through it all!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文