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
653 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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