Solved

Blur event handler gets called after form submission handler but I want it called before

Posted on 2013-11-26
5
334 Views
Last Modified: 2013-11-26
Here is a link to a simple test page to illustrate my issue:
http://www.dressorganic.co.uk/blurtest/

I have a form field in which when you enter a username it calls an AJAX post method to call a server side script to check the username is unique. This is all working fine.

Hence if you type the username "olduser" then click on the "Your name" field, you will see the blur event gets triggered and a message notifying that the username is already taken is displayed. The additional "UsernameUnique" is just a variable to show a flag has been set.

Now please click on http://www.dressorganic.co.uk/blurtest/ again and add "olduser" to the username but instead of clicking on "Your name" go straight to clicking the save button. You will notice from the alerts that the form submission gets handled first followed by the blur event handler. Unfortunately the blur event sets the "UsernameUnique" flag which the submit handler is supposed to look at. Hence the submit handler does not see UsernameUnique = false so the form gets submitted.

Can somebody please explain how I can make the blur handler get called first followed by the submit handler? Alternatively if there is a better way to handle this then please explain.
0
Comment
Question by:mike99c
  • 3
  • 2
5 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39677309
Your Blur function does actually get called first, but it's an asynchronous call (AJAX calls usually are), so your blur function gets called, and while it's off doing it's job, your code carries on into the Submit function...

You may have to change your AJAX call to be synchronous (kind of counter-intuitive but may be your only option)
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39677332
To turn you request into a synchronous request, you'll need to use the full version of the AJAX call, not just the shorthand post()

$.ajax({
  type: 'POST',
  url: "check-username.asp",
  data: { TheUsername: Username },
  success: function(result) {
    if(result !== "unique"){  
      $('#Username').focus();
      UsernameUnique = false;
      alert("Username is already taken, UsernameUnique: " + UsernameUnique)
    } else {
      UsernameUnique = true;
    }
  },
  async:false
});

Open in new window

0
 

Author Comment

by:mike99c
ID: 39677360
Thanks Chris, I have applied it here:
http://www.dressorganic.co.uk/blurtest/index2.asp

It works perfectly. I will use the long hand method on future as it makes sense.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39677372
Hey Mike,

The shorthand methods are fine if you don't need any of the advanced config options (get, post, load, getJSON etc). It's when you need to override a default setting that you'll need to go long-hand!!

Pleased you got it working - be aware though, that making your AJAX calls synchronous will cause lag in your application as the code now won't continue running until the server has responded - this may only be a couple of milliseconds but it could be a several seconds.
0
 

Author Comment

by:mike99c
ID: 39677380
Ok thanks for this. I think synchoronous i what i need in this case anyway so it works out fine and in any case the server side scripting is not likely to be too intense.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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 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)

867 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

16 Experts available now in Live!

Get 1:1 Help Now