Validating MaxLength of the TextArea using CustomValidator

In Webform1.aspx
<SCRIPT language="c#" runat=server >
void validateLength(object source, ServerValidateEventArgs args){
int num  = int.Parse(args.Value.Length.ToString());
if (num >= 4000)
{
    args.IsValid=false;
}
else
{
    args.IsValid=true;
}
}
</SCRIPT>
<asp:TextBox
	id="TextBox1"
	style="Z-INDEX: 102; LEFT: 192px;
	POSITION: absolute; TOP: 311px"
	runat="server"
	TextMode="MultiLine">
</asp:TextBox>
<asp:CustomValidator
	id="CustomValidator1"
	runat="server"
	ControlToValidate="TextBox1"
	OnServerValidate="validateLength">
	</asp:CustomValidator>

And in webform1.aspx.cs
private void Button1 Click(object sender, System.EventArgs e)
{

if (Page.IsValid)

{
       Response.Write ( "Length Proper =4000");
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>