Problem with jQuery Validation plugin: it won't validate all fields (just one) -
as can see in full code below, i'm trying use jquery's validation plugin simple validating of change password form (which appears in jquery-ui dialog box).
the form has 3 input boxes:
- existing password (required)
- new password (required, 5-20 characters)
- confirm new password (required, identical new password)
this should super easy, i'm seeing validator allows user type existing password , skip other fields. should differently? thanks!
full code
<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link href="http://localhost:8080/css/jquery-ui-1.8.6.custom.css" rel="stylesheet" type="text/css" /> <script src="http://localhost:8080/js/jquery-1.4.3.min.js" type="text/javascript" ></script> <script src="http://localhost:8080/js/jquery.validate.min.js" type="text/javascript"></script> <script src="http://localhost:8080/js/jquery-ui-1.8.6.custom.min.js" type="text/javascript"></script> </head> <body> <style> .error{ color:red; font-size:8pt; } .valid{ color:green; } </style> <form id="usersform" action="profile/save" method="post"> <a href="#" id="changepasswordlink">change password</a> </form> <div id="dialog-changepassword" title="update password"> <form id="changepasswordform"> <table> <tr> <td style="font-weight:bold;text-align:right;padding-right:1em;"> <span>current password: </span> </td> <td> <input type="password" class="currentpassword" id="currentpassword" /> </td> </tr> <tr> <td style="font-weight:bold;text-align:right;padding-right:1em;"> <span>new password: </span> </td> <td> <input type="password" class="newpassword" id="newpassword" /> </td> </tr> <tr> <td style="font-weight:bold;text-align:right;padding-right:1em;"> <span>confirm new password: </span> </td> <td> <input type="password" class="confirmpassword" id="confirmpassword" /> </td> </tr> </table> </form> </div> </body> </html> <script type="text/javascript"> $(document).ready(function() { $( "#changepasswordlink" ).click(function(){ $('#changepasswordform').validate({ success: function(label) { label.addclass("valid").html("ok!"); }}); jquery.validator.addclassrules({ currentpassword: { required: true }, newpassword: { required: true, minlength: 5, maxlength: 20 }, confirmpassword: { equalto: "#newpassword" } }); //alert(json.stringify($("#currentpassword").rules())); // alert(json.stringify($("#newpassword").rules())); // alert(json.stringify($("#confirmpassword").rules())); $( "#dialog-changepassword" ).dialog('open'); }); $( "#dialog-changepassword" ).dialog({ modal: true ,autoopen:false ,resizable:false ,width:600 ,buttons: { "update password": function() { // $("#changepasswordform input").each(function(){ // var me=$(this); // alert(json.stringify(me.rules())); // }); var errorcount=$('#changepasswordform').validate().numberofinvalids(); //$('#changepasswordform').valid() if(!errorcount && $('#changepasswordform').valid()){ $(this).dialog("close"); var userid='1'; var currentpassword=$('#currentpassword').val(); var newpassword=$('#newpassword').val(); var confirmpassword=$('#confirmpassword').val(); alert('ajax submit info now'); }else{ alert("there still "+errorcount+" errors.") } } ,"cancel": function() { $(this).dialog("close"); } } }); });//end docready </script>
i believe reason jquery validation behaving because input elements not have name attributes. give each input element unique name attribute , validate fields.
Comments
Post a Comment