I've written a JQuery validation method for checking a custom field. To check the data I call a server-side script using AJAX, which in turn, returns true or false. If false, the response will also contain an error message:
var errorMessage;
var rtErrorMessage = function() {
return errorMessage;
}
jQuery.validator.addMethod('customvalidation', function(value, element) {
var valid = true;
var url = '/validation?data=' + value;
$.ajax({
url: url,
type: 'get',
dataType: 'json',
async: false,
success: function(responseData) {
if (responseData && !responseData.isValid) {
errorMessage = responseData.errorMessage;
valid = false;
}
}
});
return valid;
}, rtErrorMessage);
This works, however turning of synchronicity means that the browser freezes during the request. This is rather annoying and JQuery even recommend against it... but what is the alternative?
Thanks in advance.
async: false? Why not allow the user to continue to fill out the form, and received the error message when the server sends you the response?remoteoption. Look at the docs..on('blur', function...method? That should remove the error you mentioned in your previous comment.