Solved

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

Posted on 2014-11-04
5
81 Views
Last Modified: 2014-11-05
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

0
Comment
Question by:-Dman100-
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
replace :
e.preventDefault();
by :
e.stopImmediatePropagation();

http://api.jquery.com/event.stopimmediatepropagation/
0
 

Author Comment

by:-Dman100-
Comment Utility
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.
0
 

Author Comment

by:-Dman100-
Comment Utility
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
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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

0
 
LVL 9

Expert Comment

by:Brian Tao
Comment Utility
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now