Best way to check minimum length for a textbox field

  • Thread starter Thread starter Abhishek Srivastava
  • Start date Start date
A

Abhishek Srivastava

Hello All,

Can I validate the minimum length of a field using any of the existing
validators?

I searched google on this subject and found people offering custom
controls for this.

However, for such a little thing it will be an overkill to bring in a
3rd party custom control. It will be great if I could use any of the
existing controls to check for minimum length.

Thanks for your help.

regards,
Abhishek.
 
How about this - it checks for 5+ alphanumeric chars:

<asp:TextBox id="txtUsername" runat="server" />

<asp:RegularExpressionValidator
id="RegularExpressionValidator1"
runat="server"
ErrorMessage="Field not valid!"
ControlToValidate="txtUsername"
ValidationExpression="[0-9a-zA-Z]{5,}" />
 
Custom Validator

private void yourValidator_ServerValidate(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args)
{
if(this.yourTextBox.Text.Length == 0)
{
args.IsValid = false;
}
 
Back
Top