About the author

Miron Abramson
Me
Software Engineer,
CTO at PixeliT
and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2014

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

A Custom Length Validator with server & client side validation

Almost evey TextBox input is going to be stored in Database (if it is not based on XML).  In the DB we need to define a maximum length for any characters field, so, we need to be sure that the input text in the TextBox in not longer then the maximum defined length in the DB where it going to be stored.

We all use Validators. I don't understand why MS didn't include in the built in controls a simple length validator (yes, I know it can be done by the RegularExpressionValidator, but why complicate things?) that simply validate maximum or minimum length of input text. Luckly, creating our custom validation control is much simplier then it sound. All we need to do is a class that inhirite from CustomValidator, override some methods, add some properties, create a JavaScript validation method, compile it into DLL and use it as any other Validator.

Override the ControlPropertiesValid() method:

private TextBox _textBox; 

 /// <summary>
 /// Check if the control is valid for validation
 /// </summary>
 /// <returns></returns>
  protected override bool ControlPropertiesValid()
  {
      Control ctrl = FindControl(ControlToValidate);
      if (ctrl != null)
      {
          _textBox = ctrl as TextBox;
          return (_textBox != null);
      }
      else
          return false;
  }

 override the EvaluateIsValid() method:

  /// <summary>
  /// Perform the validation
  /// </summary>
  /// <returns></returns>
  protected override bool EvaluateIsValid()
  {
      if ( string.IsNullOrEmpty(_textBox.Text) )
      {
          if (MinLength > 0)
              return false;
          else
              return true;
      }
      if (_textBox.Text.Length < MinLength || _textBox.Text.Length > MaxLength)
      {
          return false;
      }
      return true;
  }

 Override the AddAttributesToRender(HtmlTextWriter writer) method to support the client side validation:

  /// <summary>
  /// Add the maximum and minimum attribute for the control
  /// </summary>
  /// <param name="writer"></param>
  protected override void AddAttributesToRender(HtmlTextWriter writer)
  {
      base.AddAttributesToRender(writer);
      if (base.RenderUplevel)
      {
          writer.AddAttribute("MinLength", this.MinLength.ToString());
          writer.AddAttribute("MaxLength", this.MaxLength.ToString());
      }
  }

Register in the OnLoad event our client site script file:

  /// <summary>
  /// Add the controls script file to the page
  /// </summary>
  /// <param name="e"></param>
  protected override void OnLoad(EventArgs e)
  {
      if (Enabled && base.EnableClientScript)
      {
          Page.ClientScript.RegisterClientScriptResource(typeof(LengthValidator), "LengthValidator.LengthValidator.js");
      }
      base.OnLoad(e);
  }

 and ofcourse the client side validation method:

function ValidateLength(source, args) {
    var control = document.getElementById(source.controltovalidate);
    if( control )
    {
         args.IsValid = ( control.value.length >= source.getAttribute('MinLength') && control.value.length <= source.getAttribute('MaxLength') );
    }
}

The full class library project source  can be downloaded below. Just compile it, add it to the toolbox and use it as any other Validator

LengthValidator.zip (2.96 kb)

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Miron on Saturday, December 08, 2007 7:52 AM
Permalink | Comments (2) | Post RSSRSS comment feed

Check all CheckBoxes in GridView in client side

'Lists' controls as GridView, DataList or Repeater sometimes have a column with CheckBoxes. It is very usefull to let the user 'Select all' or 'Un Select all'. One way to do it is to loop on the control in the code behind and check/un check the CheckBoxes, or faster way is to do it on the client side, using the same idea of my preview  post:

//
//  Select or unSelect checkboxes in grid view
//
function CheckAllCheckBoxes(aspxCheckBoxID, checkVal)
{
    var reg = new RegExp( aspxCheckBoxID + "$" );
    var pageElements = document.getElementsByTagName("*");
    for(i = 0; i < pageElements.length; i++) {
        elm = pageElements[i];
        if (elm.type == 'checkbox')
        {
            if (reg.test(elm.name))
            {  
                elm.checked = checkVal
            }
        }
    }
 }

Where 'aspxCheckBoxID' is the CheckBox server side name, and 'checkVal is value if to check all or uncheck all. So simple.

Happy coding 

Currently rated 4.8 by 5 people

  • Currently 4.8/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.NET | Client side
Posted by Miron on Saturday, September 01, 2007 5:55 PM
Permalink | Comments (1) | Post RSSRSS comment feed