• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

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

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
mike99c
Asked:
mike99c
  • 3
  • 2
1 Solution
 
Chris StanyonCommented:
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
 
Chris StanyonCommented:
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
 
mike99cAuthor Commented:
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
 
Chris StanyonCommented:
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
 
mike99cAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now