Javascript validating empty fields


A few people have commented that this is all a bit confusing when it comes to implementing it on your own website.

That's hardly surprising given the mix of technologies so I've created a graphic (below) showing how the different files fit together and what goes where: You need to create two files.

We're going to be checking the form using a function, which will be activated by the form's Validating this form would be considerably simpler than one containing radio buttons or select boxes, but any form element can be accessed.

javascript validating empty fields-50javascript validating empty fields-28

This example uses the Java Script XMLHttp Request object and PHP server-side scripting to check that the Email address entered is valid (at least that it matches a particular regular expression) and that the Age lies between 16 and 100.

As the form is completed, the values are passed immediately to the server as POST variables, the server calculates a result, passing back an XML file which is then parsed to trigger various actions on the page as you will see.

The validation script returns XML data which is then processed and applied to the form to provide feedback to the user.

The other two files (represented in the graphic in green) can be downloaded or copied from this site and included from your files as indicated.

Samples of the XML files returned in each case can be found below.