preventDefault is not preventing button from calling method in my class file

I have a button that preforms a search action on my page. I'm using jquery to do a simple validation on a zip code for 5 digits. I'm using preventDefault to stop my button from running the search if the zip code is not valid. However, the preventDefault method does not seem to be working. It still runs the search even if the zip code is not validated correctly.

Here is my jquery code:

    var j$ = jQuery.noConflict();

    j$(document).ready(function() {
        var searchButton = j$('[id$=theFilterButton]');
        var isValid = true;
        searchButton.click(function(e) {
            var zip = j$('[id$="ZipCodeFilter"]').val();
            var reg = /^[0-9]+$/;
            if((zip.length) < 5 || (zip.length) > 5 ) {
                isValid = false;
                console.log("zip code length is not 5");
            }
            if (!reg.test(zip)){
                isValid = false;
                console.log("zip is not digits");
            }
            if(!isValid) {
                console.log("zip is not valid. Stop processing!!!!");
                e.preventDefault();
            }
        });     
    });

Open in new window

-Dman100-Software ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
replace :
e.preventDefault();
by :
e.stopImmediatePropagation();

http://api.jquery.com/event.stopimmediatepropagation/
-Dman100-Software ConsultantAuthor Commented:
Hi leakim971, thanks for replying to my post.  I just tried e.stopimmediatePropagation(); and that didn't work either.  The button continues to run my search regardless.
-Dman100-Software ConsultantAuthor Commented:
I did a console.log(isimmediatePropagationStopped());

and I got this in my log:

zip code length is not 5
zip is not valid. Stop processing!!!!
true
leakim971PluritechnicianCommented:
ok so don't attach the event like this :
searchButton.click(function(e) {
    // current code
});

Open in new window


use inline :

<input type="button" onclick="return myfunction();" />

and use :
<script>
function myfunction() {
   // current code
}
</script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian TaoSenior Business Solutions ConsultantCommented:
Is the searchButton a submit or just a button?  If it's a submit, you may want to use searchButton.submit instead of searchButton.click.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.