I have this jQuery Validation function issue. Validation only works once -


the function below works should. it's validate form. in short, have couple input fields. if hit submit , no text has been entered in either input, validation errors appear. if fill in 1 field, 1 of fields update. if hit "submit" again, without filling in second form input, else{} gets fired, not if{} should assure it's filled out, etc.

not familiar, return false; issue?

if ($('input[type="text"]:visible').val().length == 0) { $('.submitmodal').click(function () { $('.modal').modal('show'); }); } else { $('.submitmodal').click(function () { $('.modal').modal('hide'); }); } 

any idea im doing wrong?

this backwards thinking. bind click, period. not conditionally. on click, decide want happen (even if means ignoring click).

if bind click, filling out form differently isn't going unbind it. , though unbind it, there's no great reason to.

if .submitmodal submit input element (rather anchor or button class), instead of binding click listener, bind submit listener.

based on mike's own starting point of code, here's example (slightly outside true context, since don't have modal function available without unecessary effort!)

$('.submitmodal').click(function (e) { e.preventdefault(); $('input[type="text"]:visible').each(function() { if ($(this).val().length == 0) { $('.modal').show(); } else { $('.modal').hide(); } }); });​ 

the first thing that's different pass event click handler, , preventdefault on it. prevents anchor causing page refresh.

but other thing did swap out actual .modal() call generic jquery .show() , .hide() proof-of-concept logic.

mike, sample code wrote pretty bang-on (except preventdefault assuming in fact want prevent default anchor behaviour). here's proof:

jsfiddle.net/gnaxn/2

when visible text fields filled in, "modal" hides. when they're emptied again, "modal" reappears.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -