AJAX Mẫu Xác Nhận Các sự kiện onblur của yếu tố đầu vào, mà được tạo ra khi người sử dụng lá là một yếu tố đầu vào, gây nên các chứng thực () JavaScript chức năng với hai tham số: giá trị của lĩnh vực này và ID của trường. | AJAX Form Validation The onblur event of the input element which is generated when the user leaves an input element triggers the validate JavaScript function with two parameters the field s value and the field s ID. This function will handle AJAX validation by making an asynchronous HTTP request to the vali script. The server script needs to know which field we need to validate and what the input value is. The value attribute should be empty on first page load but after submitting the form it will hold the input value in case the form is reloaded as a result of a validation error. We use session variables to save user input on form submit in case validation fails and the form is re-displayed. The span element that follows contains an error message that gets displayed on failed validation. This span is initially hidden using the hidden CSS class but we change its CSS class into error if validation fails. Inside vali the vali date function sends an AJAX request to the server by calling vali with two parameters the field s value and the field s ID. Remember that XMLHttpRequest cannot make two HTTP requests at the same time so if the object is busy processing a previous request we save the details of the current request for later. This is particularly useful when the connection to the network or the Internet is slow. The request details are saved using a cache system with the properties of a FIFO structure. Luckily the JavaScript s Array class offers the exact functionality we need through its push and shi ft methods and hence we use it for caching purposes var cache new Array So validate starts by adding the data to validate to the cache if the function received any . the function handles the validation for any form field function validate inputValue fieldlD only continue if xmlHttp isn t void if xmlHttp if we received non-null parameters we add them to cache in the form of the query string to be sent to the server for validation if fieldlD .