Tags: , , , | Categories: development Posted by pieterg on 3/18/2010 8:44 AM | Comments (18)

Never trust user input!

This week at MIX10, Phill Haack had a presentation on what is new with ASP.NET MVC 2.0. The following is a list of the new features in ASP.NET MVC 2.0 from Scott Guthrie’s blog

I have recently been tinkering with an amazing validation engine in jquery from Cedric Dugas. I thought to myself, what an awesome idea it would be to try and minimize the effort of adding the validation engine to the Model Validation inside ASP.NET MVC 2.0.

The first step was to determine where the validation was taking place.

Inside of jquery.validate.js, I found the formatAndAdd function on line number 574. After the error message is constructed, I added the validationEngine.buildPrompt function which is a way for developers to utilize the nice looking prompt that is packaged as part of the plugin.

    formatAndAdd: function(element, rule) {
        var message = this.defaultMessage(element, rule.method),
    	theregex = /\$?\{(\d+)\}/g;
        if (typeof message == "function") {
            message = message.call(this, rule.parameters, element);
        } else if (theregex.test(message)) {
            message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters);
        }
        $.validationEngine.buildPrompt(element, message, "error");
        this.errorList.push({
            message: "",
            element: element
        });
    
        this.errorMap[element.name] = message;
        this.submitted[element.name] = message;
    },
    

Step two to determine where the errors are removed, when they have passed the validation rule.

I found an unhighlight function on line 259 of the jquery.validate script. This code doesn’t remove the error, but it does remove the highlighting from the control. I decided to tie into this piece of code by adding the closePrompt method from the validation engine.

unhighlight: function(element, errorClass, validClass) {
    $.validationEngine.closePrompt(element);
    $(element).removeClass(errorClass).addClass(validClass);
}

The final step is to initialize the validation engine.

on line 50, after the submit button was clicked, we will initialize our validation engine.

if (validator.settings.onsubmit) {
    $(validator.currentForm).validationEngine();

prettyaspnetmvcvalidation

Here is the sample code. To utilize this code, all you will need to do is get the latest version of the Validation Engine and replace the jquery.validation.js with this one.

jquery.validate.js (48.89 kb)

ValidationDemo.zip (391.38 kb)

Update : An Update from the Validation Engine guru over at position-absolute now removes the box when you click on it, if it’s in the way.

Comments

pingback
position-absolute.com on 2/15/2010 4:52 AM Pingback from position-absolute.com

A jQuery inline form validation, because validation is a mess «  Position Absolute, resources for the web developer
w3cvalidation
w3cvalidation United States on 3/1/2010 1:35 AM Nice information, I really appreciate the way you presented.Thanks for sharing..

Johan
Johan Sweden on 3/12/2010 3:39 PM How does this tie in with model validation?

What is the difference you've made compared to the original code?

Could you give an example on how it works with a model?

I don't see any difference in how it works compared to the original code.

Thank you.
Johan
Johan Sweden on 3/13/2010 1:30 PM To put my first post in a bit more simple, what do you have to do to make a ModelState error show as a plugin popup?
PieterG
PieterG South Africa on 3/21/2010 12:35 AM Hi Johan,
Currently, the Model Validation has the ugly look and feel tied to it and I decided to use this validation engine and tie it into the Model Validation that is currently part of ASP.NET MVC.

So you would still write the following code
<% Html.EnableClientValidation(); %>
<%= Html.TextBoxFor(model => model.ID)%>
<%= Html.ValidationMessageFor(model => model.ID)%>
but have the nice looking JQuery validation engine.
dissertation help
dissertation help United States on 3/21/2010 8:32 AM Your blog is rich in valuable information. It helps me better understand the programming and web development.
seo services
seo services United States on 3/22/2010 11:56 AM Thanks for giving us the information about the features of ASP.Net.
seo services
seo services United States on 3/22/2010 12:01 PM Microsoft's ASP has become synonymous with ASP.NET which is a developer's dream come true.  However, it's important to note that ASP.NET is all new technology; it's not a rehashed and upgraded ASP program.  It is software that is made to run on a server, not on someone's laptop.  The software itself enables developers to build other programs - it technically isn't a stand alone program that produces its own results.
seo company
seo company United States on 3/25/2010 5:43 PM Useful info. Hope to see more good posts in the future.
debt consolidation loans
debt consolidation loans United States on 3/25/2010 10:44 PM Debt Mediators assists those in financial hardship with bankruptcy, debt consolidation, loans, payment strategies and other debt solutions
link building
link building United States on 3/28/2010 12:52 AM The ASP.NET methodology has been a huge leap in the way web applications are developed and deployed. Previously in traditional ASP, code was cluttered and there was no clear and simplified way in which the benefits of object oriented programming could be brought to the web. In addition, there was no clear separation between the presentation layer and business logic, making maintenance of code a big issue.
online surveys for cash
online surveys for cash United States on 3/28/2010 10:47 AM Is there anyway to make any money while on bed rest?
locksmith center
locksmith center Dominican Republic on 3/30/2010 4:15 PM Hey about asp.net i do believe your weblog is amazingly F - First-rate i discovered it in aol and i set it on my favorite list  plan to view extra wonderful posts from you  soon.
Ticket printing
Ticket printing United States on 3/31/2010 5:49 AM Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
Jean Fossa
Jean Fossa Kazakhstan on 4/2/2010 6:22 AM This is exciting, post more often!
Cure Premature Ejaculation
Cure Premature Ejaculation United States on 4/6/2010 5:04 AM Now this may be an awesome web internet site with data a lot more along with a good deal of folks are attempting for. I'm going to truly arrive back again back again a lot of usually.
debt consolidation
debt consolidation United States on 4/6/2010 8:47 AM Thanks for all the information. On a side note, how do you deal with all the spammers you get on your weblog? My recent weblog is getting invaded by random spammers and I have been spending a lot of time removing comments its simply not funny. . I get so tired combating spam that I feed I ought to stop blogging entirely.
pingback
53.jordanbrandallamerican.com on 4/16/2010 6:39 AM Pingback from 53.jordanbrandallamerican.com

Ml63 Amg Odm Salvage, Ml63 Amg Brake Caliper Parts

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading