Using the remote function of jquery validation
I realize this is old but I had a hard time getting this working as well and wanted to share what worked for me.
Client-Side form validation code:
$('#frmAddNew').validate({
onkeyup: false,
rules: {
ADID: {
required: true,
alphanumeric: true,
maxlength: 10,
remote: {
url: "ADIDValidation.cshtml",
type: "post",
dataType: "json",
dataFilter: function (data) {
if (data) {
var json = $.parseJSON(data);
if (json[0]) {
return JSON.stringify(json[0].valid) /* will be "true" or whatever the error message is */
}
}
},
complete: function (data) {
/* Additional code to run if the element passes validation */
if (data) {
var json = $.parseJSON(data.responseText);
if (json[0]) {
if (json[0].valid === "true") {
$('#FirstName').val(json[0].FirstName);
$('#LastName').val(json[0].LastName);
}
}
}
}
}
}
},
messages: {
ADID: {
required: "Please Enter an ADID of the Employee",
alphanumeric: "The ADID Can Only Contain Letters and Numbers",
maxlength: "ADID For User's Current Permissions Must Be 10 Characters or Less"
}
},
submitHandler: function (form) {
form.submit();
},
errorContainer: $('section.errorCon'),
errorLabelContainer: $('ol', 'section.errorCon'),
wrapper: 'li',
showErrors: function (errors) {
var error = this.numberOfInvalids();
var message = error == 1
? 'You missed 1 field:'
: 'You missed ' + error + ' fields:';
$('section.errorCon h3').html(message);
this.defaultShowErrors();
}
});
Server-Side code for ADIDValidation.cshtml (I'm using Razor webpages):
@{
Layout = null;
if(IsPost)
{
var db = Database.Open("<enter connection name>");
var sql = "<enter sql or stored procedure>";
var strADID = Request["ADID"];
var result = db.Query(sql, strADID);
IEnumerable<dynamic> response;
if(result.Any())
{
@* "true" indicates the element passes validation. Additional data can be passed to a callback function *@
response = result.Select(x => new
{
valid = "true",
FirstName = x.FirstName,
LastName = x.LastName
});
}
else
{
@* The element did not pass validation, the message below will be used as the error message *@
response = new[] {
new {valid = "The Employee's ADID '" + strADID.ToString() + "' Is Not Valid. Please Correct and Resubmit"}
};
}
Json.Write(response, Response.Output);
}
}
I know it's too late, but this could help other people.
The remote method is meant to recieve a Json string, so your server side should be returning something like this to the method...
echo(json_encode(true)); // if there's nothing matching
echo(json_encode(false));
This is an example of the JS code that I wrote while trying to validate user's nickname.
$(document).ready(function(){
$('#form').validate({
rules: {
user_nickname: {
remote: {
url: "available.php",
type: "post",
data: {
user_nickname: function() {
return $( "#user_nickname" ).val();
}
}
}
}
},
messages:{
user_nickname: {
remote: "Username already taken"
}
}
});});
Hope it helps someone, it helped me.
The easiest way to accomplish this is to simply return true
, an error message as a string, or false
from your server-side resource. According to the jQuery validate documentation for remote:
The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.
This means if you can change your server-side code to return true
in the event of successful validation or an error message ("username already in use") in the event of unsuccessful validation, you could just write the following remote rule:
remote: {
type: 'post',
url: 'register/isUsernameAvailable',
data: {
'username': function () { return $('#username').val(); }
},
dataType: 'json'
}
You could also simply return true
or false
from your server-side resource and define the error message on the client. In that case you would have the above rule and then a property in the messages
object:
messages: {
username: {
remote: "username already in use"
}
}