ASP.NET MVC and JQUERY Validation

Recently I have been using this nice library that leverages the MS Validation Application Block along with the JQuery Validation plugin.

http://www.codeplex.com/aspmvcvalidation.  Really nice solution.

I wanted to extend this a bit by leveraging the remote feature in the JQuery validation block.  This was for cases like when we need to check  if a login is already taken.  In short I wanted a mechanism to emit out the JQuery validation that can call our server side validation method.  The JQuery validation plugin has a nice hook for us in the remote keyword.

To make this happen I added the following classes to the MVC.Validation Library.

using System;

using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace Mvc.Validation.Validators
{
/// <summary>
/// Logs an error if the string to validate is <see langword=”null”/> or empty.
/// </summary>
public class RemoteValidator : ValueValidator
{
private string _RemoteURL;
public RemoteValidator()
: this(null)
{ }

public RemoteValidator(string messageTemplate)
: base(messageTemplate, null, false)
{ }

public RemoteValidator(string messageTemplate, string RemoteURL)
: base(messageTemplate, null, false)
{
this._RemoteURL = RemoteURL;
}

protected override void DoValidate(object objectToValidate,
object currentTarget,
string key,
ValidationResults validationResults)
{
string stringValue = null;
if (objectToValidate != null && !(objectToValidate is string))
{
stringValue = Convert.ToString(objectToValidate);
}
else
{
stringValue = (string)objectToValidate;
}
if (string.IsNullOrEmpty(stringValue))
{
LogValidationResult(validationResults, GetMessage(objectToValidate, key), currentTarget, key);
}
}

protected override string DefaultNonNegatedMessageTemplate
{
get { return “Element must not be null or empty”; }
}

protected override string DefaultNegatedMessageTemplate
{
get { return null; }
}
}
}

and

using System;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace Mvc.Validation.Validators
{
[AttributeUsage(AttributeTargets.Property
| AttributeTargets.Field
| AttributeTargets.Method
| AttributeTargets.Class
| AttributeTargets.Parameter,
AllowMultiple = false,
Inherited = false)]
public sealed class RemoteValidatorAttribute : ValueValidatorAttribute
{
string _RemoteURL;
string _MessageTemplate;

public RemoteValidatorAttribute(string MessageTemplate)
: base()
{
this._MessageTemplate = MessageTemplate;
}

public RemoteValidatorAttribute(string RemoteURL, string MessageTemplate)
: base()
{
this._RemoteURL = RemoteURL;
this._MessageTemplate = MessageTemplate;
}

public string URL {
get
{
return this._RemoteURL;
}
set
{
}
}

public string MessageTemplate
{
get
{
return this._MessageTemplate;
}
set
{
}
}

protected override Validator DoCreateValidator(Type targetType)
{
return new RemoteValidator(this._MessageTemplate, this._RemoteURL);
}
}
}

When combined with this validation library, we get the following javascript code emitted to our page.  This example validates that an EmailAddresss is not already taken.  It does so by calling our Controller called /ServerValidation/IsValidLogin.

$("#EmailAddress").rules("add", {
	required: true,
	remote: "/ServerValidation/IsValidLogin",
	messages: {
		required: "Email Address is Required.",
		remote: "Login is already in use."
	}
});

Nice Solution and thanks for the library buunguyen

3 thoughts on “ASP.NET MVC and JQUERY Validation

Leave a Reply