I’m excited to release version 0.9.7 of validanguage. Here’s a breakdown on what’s new:
- Added support for the new isAjax attribute. This enables you to add the isAjax attribute to your validation functions and have them treated as an ajax call, instead of a standard function. More on this below.
- Added hooks for dojo and jQuery in validanguage.useLibrary
- Bug Fix: Fixed the assignment of validateCharacters() events
- Removed deprecated validateUSDate() function — If you hadn’t yet updated your code, you should be using validateDate() instead.
Also, I made some changes to the Validanguage Custom Download page. You can now deselect validanguage features which you don’t plan on using to shrink the size of the library. And, thanks to Henrik, Danish error messages are now offered.
The big change with this new release is the new ajax support. Previously, ajax validation was supported only indirectly, via the ability to manually flunk fields and manually call the onerror functions. With the new release, ajax functions are more tightly integrated into the existing validation framework.
The new ajax validation works as follows. When the code hits a validation function with isAjax = true associated with it, the code will fire off the ajax request and wait on the response to come back from the server. Once the response has come back, it will pick up where it left off (this is accomplished through the use of setInterval to create a loop that continually checks to see if the results have been reported back). This allows you to include custom ajax functions within an onsubmit form validation loop, just like you would with any other function.
Additionally, the following features are supported for ajax validations:
- Customizable timeouts for expired requests. By default, if a form submission is waiting on results for an ajax validation and 30 seconds have elapsed, the form will automatically submit. Thus, users will not be prevented from submitting the form due to network issues or other problems with the ajax call.
- Result caching. Once an ajax request comes back and returns results for a given form field’s value, any subsequent validation of the same value will check the cache, instead of triggering a new request.
- Discarding of outdated requests. One of the challenges in dealing with ajax is that there is no guarantee that requests will come back in order. Thus, if you’re checking for the availability of a username and the user initially submits “dan” and then submits “dan17″ a few seconds later, it is not unheard of for the “dan17″ results to come back first, followed later by the “dan” results. Validanguage supports disregarding any older results through the use of an ajaxCounter that is incremented with each request. This counter will be sent to your validation callbacks so they can determine whether or not they are still relevant.
Timeouts and result caching can be disabled, if desired, via validanguage.settings.submitFormOnExpiredAjax and validanguage.settings.cacheAjaxLookups.
Since I have not yet gotten to update the documentation for the new ajax functionality, you can refer to the Ajax validation Test Cases page for an example of how to set this up. The important parts are the following 2 functions:
- validanguage.setValidationStatus(id, returnStatus, type, errorMsg) — Call this function inside your ajax callback to tell Validanguage that the ajax check has returned.
- validanguage.isExpiredAjax(formFieldId, ajaxCounter) — This function examines the ajaxLookup array to determine whether or not the specified ajaxCounter pertains to the most recent ajax call for that form field.
The new ajax support is primarily for advanced users who have a knowledge of server-side programming. In order to utilize the caching and discarding of outdated requests you will need to integrate the ajaxCounter variable into your ajax calls so that it is sent to the server and then returned inside the response (most likely via JSON encoding). To enable this, any function labelled with isAjax will be called with the form field value as the first argument and an incremented ajaxCounter value as the second argument.
That’s about it for now. I know these instructions are pretty sketchy, but I hope to have some more details online shortly. Feel free to contact me with any questions.