Solved

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

Posted on 2009-05-09
4
660 Views
Last Modified: 2013-11-26
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?

0
Comment
Question by:meetpd
4 Comments
 
LVL 6

Accepted Solution

by:
hehdaddy earned 500 total points
ID: 24346596
Hi there. You can use the ASP.NET regular expression validator control to enforce the length. Add one to your page and set the regular expression to be: ^.{7}$

Now, if the user tries to leave the textbox without putting in exactly 7 characters, you can display an error if you want, and keep them in there.
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24347950
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!


0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24348047
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24358864
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.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question