开发者

how to use conditional validation in jquery validater plugin to my form

开发者 https://www.devze.com 2023-04-01 12:57 出处:网络
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

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: {
                    requ开发者_JAVA百科ired: 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 ............................. :(


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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消