Created By Adrian Statescu

AJAX Form Validation

This degradable form validation demo verifies the form at the server side on the classic form submit, and also implements AJAX validation while the user navigates through the form.

Real-time server validation using AJAX

Register
  I`ve read the Term of Use

Note: All fields are required.

Exercise Explanations: AJAX Form Validation and Thread-Safe AJAX

This degradable form validation demo verifies the form at the server side on the classic form submit, and also implements AJAX validation while the user navigates through the form. The final validation is performed at the server, as shown in the following diagram

Doing a final server-side validation when the form is submitted is always a must. If someone disables JavaScript in the browser settings, AJAX validation on the client side won't work, exposing sensitive data, and thereby allowing an evil-intended visitor to harm important data back on the server (for example, through SQL injection). Always validate user input on the server!

This form validation application validates a registration form, using both AJAX validation (client side) and typical server-side validation:

Both AJAX validation and PHP validation check the entered data against these rules:

The hidden danger behind this technique is only revealed if the user moves very quickly through the input fields, or the server connection is slow; in these cases, the web application would attempt to make new server requests through an XMLHttpRequest object that is still busy waiting for the response to a previous request (this would generate an error and the application would stop functioning properly).

CREATE TABLE `validate_users`
(
`id` int(10) unsigned NOT NULL auto_increment,
`username` varchar(32) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `username` (`username`)
) ENGINE=MyISAM;
INSERT INTO `validate_users` VALUES (1, 'lancia');
INSERT INTO `validate_users` VALUES (2, 'larson');
INSERT INTO `validate_users` VALUES (3, 'filip');
INSERT INTO `validate_users` VALUES (4, 'adrian');
INSERT INTO `validate_users` VALUES (5, 'snake');