Html.ValidateTextBox

OK.  So why is it, that most of the validation examples I see for ASP.NET MVC always include allowing the user to submit potentially invalid form fields to the server?? 

If your validation "framework" requires you to submit a form to the server prior to being validated, then your framework sucks.  Period.  Definitely have the server side validation.  Server side validation exists to double check that you didn't fuck up and miss something on the front end with client side validation.

I implemented a VERY lame outline of something that I would expect from a framework.  Simple attributes are applied to properties that are bound in the view.  My example expects that you are using jQuery.  This is not good. 

A validation framework that works on the client, should not care about it's implementation but should have a default implementation. So it shouldn't care if you're a jQuery fan, or prototype fan, or not a fan of javascript at all;it should establish some common methods that you are free to implement yourself.

If Microsoft were to implement a good validation framework, here's what I would require from them.  Zero installation.  Requiring that you install the Enterprise Library to get any sort of validation bennies is absurd, please stop doing this.

The framework would default to Microsoft's home grown javascript library, but with VERY little effort, could be switched out with prototype, jQuery, or any javascript framework you want.

Here's my scratch project trying to come up with a better validation framework.  It's tied to jQuery, which I don't like.  It looks for any element(i've only concentrated on an input element of type="text", so it doesn't do much) that contains a 'validate' attribute.  then inspects other css classes that have been tacked on that indicate some sort of validation to perform on the element.  I'm thinking of also sticking other attributes on the element when it's generated, not XHtml-ish, but oh well.

It's based on attributes that implement my IModelValidator interface which looks like this:

public interface IModelValidator
{
  bool Validate(object value);
}
1
2
3
4

The ValidateRequiredAttribute tests to make sure a value exists and looks like this:

public class ValidateRequiredAttribute : System.Attribute, IModelValidator
{
  public bool Validate(object value)
  {
    if (value == null) return false;
    if (System.Convert.ToString(value).Length == 0) return false;
    return true;
  }
}
1
2
3
4
5
6
7
8
9

ValidateMaxlengthAttribute checks to make sure the length of a value does not exceed a max value:

public class ValidateMaxLengthAttribute : System.Attribute, IModelValidator
{
  private int _maxLength = 0;
  public int MaxLength
  {
    get
    {
      return _maxLength;
    }
  }
  public ValidateMaxLengthAttribute(int maxLength)
  {
    _maxLength = maxLength;
  }
  public bool Validate(object value)
  {
    if (value == null) return false;
    return System.Convert.ToString(value).Length
 
Author: , 0000-00-00