?
Solved

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

Posted on 2014-11-04
5
Medium Priority
?
89 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 1500 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

801 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