Leveraging Validator Toolkit for ASP.NET MVC to Build Client and Server-Side Form Validation

Validator Toolkit for ASP.NET MVC

Validator Toolkit for ASP.NET MVC

After having gained an incredible amount of popularity as one of the finest web development frameworks, Asp.net is now available with a validator toolkit that enables the developers to build both, client side and server-side form validation. The MVC programming model available for Asp.net has made its mark in the world of Asp.net development. In today’s blog, I’ll be covering details about the validator toolkit that’s available for Asp.net developers.

What’s the Validator Toolkit for Asp.net MVC used for?

While using Asp.net, the developers need a solution for validating an HTML form on the client and server side. Thanks to the existence of the Validator Toolkit for Asp.net MVC, it has become possible for the developers to perform form validation on the client and server side using the validation sets. As a new project, the Validator toolkit for Asp.net MVC has been uploaded on Microsoft’s Open Source Community site at http://mvcvalidatortoolkit.codeplex.com/ .

Validator toolkit for Asp.net MVC- How does it work?

The Validator toolkit uses validation sets as specific elements. These validation sets are the special classes that are derived from the ValidationSet base class and define all the validation rules for a particular HTML webpage form. The validator toolkit allows you to generate client-side JavaScript code on the basis of validators that have already been defined during the view rendering process. Talking specifically about client side validation, the validator toolkit allows the client side to use the very powerful jQuery JavaScript library in collaboration with the jQuery validation plug-in for accomplishing the task of client validation. This validation plug-in is customized for supporting the behavior that’s expected by the developer. Apart from using the jQuery library for client-side HTML form validation, it can also be used for creating a lot of other exciting stuff including animations, special effects etc.

Validator toolkit for Asp.net MVC- Understanding it better

Here’s a look at a sample validation set:

public class LoginValidationSet : ValidationSet {

 
   protected override ValidatorCollection GetValidators() {

      return new ValidatorCollection (

         new ValidateElement("username")

           { Required = true, MinLength = 5, MaxLength = 30 },

         new ValidateElement("password")

           { Required = true, MinLength = 3, MaxLength = 50 }

     );

   }

}

In the above code, the LoginValidationSet class will define the rules for validating a simple login form. This will be done by overriding the GetValidators method of the base class which will return a ValidatorCollection instance along with all the validators that are required for validating the HTML form at a later point of time. Under such a situation, the username field will be required and input for the same will contain a minimum of 5 characters and a maximum of 30 characters. The developer would also be required to enter a value for the password field, restricting the count of characters between 3 and 50.

An uncertainly associated with the use of Validator toolkit for Asp.net

If by any chance the Validator toolkit uses custom attributes for setting the validation rules then there’s actually no guarantee for the HTML form validation process to validate in exactly the same order as the attributes have been designed. In addition to this, since the Type.GetCustomAttributes method will return the attribute list in an alphabetical order, this uncertainty become even more rigid. As someone who’s looking forward to validate the HTML forms on client and server side, you may also choose to write your own custom validators or opt for using the ValidateScriptMethod validator for calling a specific JavaScript function on the client side in addition to defining a method within the validation set class for validating the HTML form for the server-side.

Validator Toolkit brings in the convenience of creating custom validators

With a handy validator toolkit, you can easily create custom validators with just some basic knowledge about the jQuery JavaScript library and the validation plug-in. Every sample site created using validator toolkit includes a custom validator that’s named as ValidateBuga. This validator checks the input value against a constant string that’s called a buga. With each validator deriving from a specific Validator class, the custom validator provides a couple of virtual methods.

Wrapping Up

Now that was a round up of the basic features and working the validator toolkit for Asp.net MVC that’s used for performing HTML form validation for client and server side. Hope the details covered above would serve as a handy guide during all your forthcoming form validation.

About the Author :

Celin Smith is an ASP.Net web developer and blogger at Xicom Technologies Ltd. who loves to write about web & mobile apps. If you are looking to Hire .Net MVC Developers, you can get in touch with her or directly contact Xicom.