05 May

jQuery 1.3 With PHP: chapter 4

Chapter 4 (of my book jQuery 1.3 with PHP for Packt) was on form validation. I put up the demos in the usual place.

Of particular interest is this one. It has a number of points, including two that I haven’t seen anywhere else yet.

  • remote checking of email addresses against the server database (kae@verens.com is registered, for example).
  • the Country drop-down is a new trick which I’ll explain in a moment.
  • the City field acts as an auto-suggest based on what’s selected in Country (select Ireland and type a letter in there).
  • the form itself does not have an action parameter. that’s the second trick.

Optimising Large Select-boxes

In the example, I’ve only mentioned a few countries, but in reality, you’d use the full list. If you look at the source of the form, you’ll see that actually, only Ireland is mentioned. This is because it does not make sense to always print tens of KB of HTML if most of it will not be used – what if the form is for an irish company and it is not expected that the country would /need/ to change?

The solution is to only load the country list if the select-box is clicked.

This also applies to other large select-boxes. I’ll be using this trick in-house for forms that always slow down when they’re rendering select-boxes – especially db-sourced select-boxes.

Action-less Forms

This is an attempt to get away from captchas, which are getting increasingly difficult for humans to solve, and easier for computers.

The idea is that the form is rendered with no action parameter, and then jQuery retrieves the needed parameter via AJAX 1.5 seconds later. The server records the time the form was rendered, and if the jQuery request comes in before 1.5 seconds, then it’s a spam-bot and will not be allowed to post. If the form is submitted in less than 10 seconds, or greater than 10 minutes, or if it’s submitted without loading the form and action in the first place, then it’s a spam bot and will not be allowed to post.

I’m pretty confident this will slow down spam a bit. It can’t possibly /solve/ it, but it will make it more difficult for spam-bots, yet humans will not notice anything out-of-the-ordinary at all.

The source is of course available.

Leave a Reply