Monday, August 23, 2010

Create MaxLength on Textbox

Using the simple script below, multi-line textboxes will prevent a user from exceeding the MaxLength property.

* Note - using System.Text;

StringBuilder funcStr = new StringBuilder();
funcStr.Append("function isMaxLength(txtBox) {");
funcStr.Append("if(txtBox){");
funcStr.AppendFormat("return(txtBox.value.length <= {0});", aTextBox.MaxLength);
funcStr.Append("}}");

aTextBox.Attributes.Add("onkeypress", "return isMaxLength(this);");

ClientScript.RegisterClientScriptBlock(
   this.GetType(),
   "txtLength",
   funcStr.ToString() , true);

1 comment:

Anonymous said...

does not allow to edit once max characters are inserted.