/ Published in: jQuery
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<script> $(document).ready(function(){ $("#myform").validate({ groups: { username: "fname lname" }, errorPlacement: function(error, element) { if (element.attr("name") == "fname" || element.attr("name") == "lname" ) error.insertAfter("#lastname"); else error.insertAfter(element); }, rules: { fname:{ required:{ depends: function(element){ var status = false; if( $("#yesname:checked").val() !== undefined){ var status = true; } //console.log("yesname is: "+status); return status; } } }, lname:{ required:{ depends: function(element){ var status = false; if( $("#yesname:checked").val() !== undefined){ var status = true; } //console.log("yesname is: "+status); return status; } } } }, debug:true }) }); </script> <form id="myform"> <label for="name">Do you have a name?</label><br> <input type="checkbox" name="name" value="y" id="yesname">yes<br> <input type="checkbox" name="name" value="n" id="noname">no<br> <p>if you have a name give it to me!</p> <label>Your Name</label> <input name="fname" id="firstname" placeholder="first name..." /> <input name="lname" id="lastname" placeholder="last name..." /> <br/> <input type="submit" value="Submit"/> </form>
URL: http://bassistance.de/jquery-plugins/jquery-plugin-validation/