Solved

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

Posted on 2014-11-04
5
87 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40422199
replace :
e.preventDefault();
by :
e.stopImmediatePropagation();

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

Author Comment

by:-Dman100-
ID: 40422207
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-
ID: 40422219
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
ID: 40422262
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
ID: 40423137
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

688 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