简体   繁体   English

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

[英]how to use conditional validation in jquery validater plugin to my form

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 , change意味着隐藏或显示,因此根据下拉值隐藏和显示表单字段,我需要向此表单添加j查询验证插件,

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

I think you can use the visible selector. 我认为您可以使用可见的选择器。

http://api.jquery.com/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. 那么当您调用validate()方法时,如果该文本框处于隐藏状态,则不会对其进行验证。 The trick is to add the "required" class to the class attribute 技巧是将“必需”类添加到class属性

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; 因此,当创建一个新的时,我放置了一个javascript变量var newUser=true; else editing existing user var newUser=false; 否则编辑现有用户var newUser=false;

Then in my rules portion of my validate I put required: (newUser)?true:false 然后在我的验证的规则部分中,我required: (newUser)?true:false

This causes validate to validate the username field only if new user is true . 这导致只有在新用户为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. 如果我们加载了现有用户,则只需在PHP代码中添加javascript var,规则的required部分便将具有false值。

Remember, you can put javascript into a json object. 请记住,您可以将javascript放入json对象。 Because it's all javascript! 因为都是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//articles/DOMM-7LZJNN7 //文章详细介绍了如何真正使用验证。 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 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! 仅在http://docs.jquery.com/Plugins/Validation中 ,信息的深度也非常惊人……您只需单击全部即可!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM