Best way to check minimum length for a textbox field

  • Thread starter Abhishek Srivastava
  • 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.
 
D

Dan

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,}" />
 
C

Curt_C [MVP]

Custom Validator

private void yourValidator_ServerValidate(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args)
{
if(this.yourTextBox.Text.Length == 0)
{
args.IsValid = false;
}
 
Joined
Mar 10, 2009
Messages
1
Reaction score
0
Best solution is here.

From another site i found this:

<script type="text/javascript" language="javascript">

//redefine the scroll to function because someone at ms decided it was a good idea to force the page to scroll all the way back up

//when clientside validation fails

window.scrollTo = function( x,y ) { return true; }

</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top