jquery - Validate form before adding an extra text fields -
if check fiddle here, there form validated properly. can click on submit button check how works.
and there button called add person,which creates group of text fields.
my question is, add person button should create text fields when form filled. if form not filled , user clicks on add person button should show alert.
here code
$.validator.setdefaults({ submithandler: function() { alert("submitted!"); } }); $().ready(function() { // validate comment form when submitted $("#commentform").validate(); // validate signup form on keyup , submit $("#signupform").validate({ rules: { firstname: "required", lastname: "required", email: { required: true, email: true }, topic: { required: "#newsletter:checked", minlength: 2 }, agree: "required", 'country': { required: true } }, messages: { firstname: "please enter firstname", lastname: "please enter lastname", username: { required: "please enter username", minlength: "your username must consist of @ least 2 characters" }, email: "please enter valid email address", agree: "please accept our policy", country: "please select option" } }); clicks = 0; $('a').on('click', function () { $('.attnd2').show(); $('div.loop').show(); if ($('div.loop').length < 5) { clicks++; if(clicks>1){ newloopdiv = $($('div.loop')[0]).clone().appendto(".container"); $('input', newloopdiv).each(function (index, element) { $(element).attr('name', $(element).attr('name') + clicks); }); } else{ newloopdiv = $($('div.loop')[0]).appendto(".container"); $('input', newloopdiv).each(function (index, element) { $(element).attr('name', $(element).attr('name') + clicks); }); } } }); });
you should add rules
dynamically created elements
like
$.validator.setdefaults({ submithandler: function(frm) { var flag=true; var $allelemreq=$("input[required],select[required]"); for(var ind=0;ind<$allelemreq.length;ind++) { elem=$allelemreq[ind]; if(!$(elem).val()) { flag=false; $(frm).validate().element($(elem)); break; } } if(flag) alert("submitted!"); else alert('not submitted'); } });
additional add required attribute
compulsory fields
fiddle: http://jsfiddle.net/rw9ns/22/
Comments
Post a Comment