Link to home
Start Free TrialLog in
Avatar of meetpd
meetpdFlag for India

asked on

Need to have textbox with input equal to 7 digits only - no more - no less (ASP.NET C#)

I have a textbox where it is required to have 7 digits inputs only. User cannot enter less than 7 digits and more than 7 digits.

I understand that in Textbox properties we can specify max characters, but how do we specify min characters for a textbox?

ASKER CERTIFIED SOLUTION
Avatar of hehdaddy
hehdaddy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tillgeffken
tillgeffken

First of all there's no min length property for textboxes but you can use a combination of validators and extenders to simplify filling out your form.

First of all set the MaxLength property of the TextBox to 7.

Then, if using the ajax control toolkit is an option, add a filtered textbox extender to it which will only allow users to enter digits into the textbox. (Example)

Then, for serverside validation, add a CustomValidator to it with the ValidateEmptyText property set to True and use the OnServerValidate event to handle the validation. You can validate via a int.TryParse and value.Length == 7 check if regular expressions are not your friend.

Then for client side validation check if http://www.livevalidation.com/ is an option for you. They have nice validators that indicate when input is satisfactory like coloring the textbox green and displaying a text when the input is acceptable.

Good Luck!


Avatar of Éric Moreau
First, add a RequiredFieldValidator to validate the textbox to make sure it is not empty.
Then, add a RegularExpressionValidator with the ValidationExpression="^\d{7}$" to force user to enter 7 digits only before they can submit a form.