How to Return Custom Error Messages with JQuery Form Validations ‘Remote’ Validator

Posted (Updated ) in Javascript

It appears JQuery Form Validation supports returning an error string when using the remote validator but it’s not immediately clear how to do this and the examples tab doesn’t have an example for doing so. Getting this working is actually very easy. All you need to do is return a JavaScript string (complete with quotations) in your response. Here’s an example:

Firstly, the javascript:

$(document).ready(function() {
		rules: {
			username: {
				required: true,
				remote: "/your/url.php"
		messages: {
			username: "Username is required."

Pretty standard, nothing out of the ordinary here. Now for the server side response – be sure to remember the quotations and relevant escaping!:

// /your/url.php
echo '"This username is already taken. Please enter a different username and try again."';