html - jQuery validation plugin - pass function as parameter -
i'm writing jquery plugin validate forms. plugin works great want able define happens after form validates. want able pass function parameter. function contain bunch of stuff submit form. parameter needs called when validation successful.
plugin called within html file:
<script> $(document).ready(function(){ $('#cform').shdvalidate({ success : formsubmit() }); }); </script>
jquery plugin:
(function($) { $.fn.shdvalidate = function(options) { //==== settings var shdvalidatesuccess = $.extend(options).success, form = this; //==== submit click this.children('input[type=submit]').click(function(){ //variables var shdrequired = $(form).children('.required'), shdvalid = 0; //validated fields $(shdrequired).each(function(){ $(this).removeclass('shdvalidatealert'); if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')){ $(this).addclass('shdvalidatealert'); } else { shdvalid += 1; } }); //outcome if (shdvalid == $(shdrequired).length) { //the success parameter needs called here somehow } return false; }); } }(jquery));
as can see, i've commented parameter needs called in plugin. @ moment can't working.
you need make 2 changes
$(document).ready(function(){ $('#cform').shdvalidate({ success : formsubmit }); });
and
(function($) { $.fn.shdvalidate = function(options) { //==== settings var shdvalidatesuccess = $.extend(options).success, form = this; //==== submit click this.children('input[type=submit]').click(function(){ //variables var shdrequired = $(form).children('.required'), shdvalid = 0; //validated fields $(shdrequired).each(function(){ $(this).removeclass('shdvalidatealert'); if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')){ $(this).addclass('shdvalidatealert'); } else { shdvalid += 1; } }); //outcome if (shdvalid == $(shdrequired).length) { //the success parameter needs called here somehow if($.isfunction(shdvalidatesuccess)){ shdvalidatesuccess(form); } } return false; }); } }(jquery));
Comments
Post a Comment