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
632 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
Comment Utility
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
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
0
 
LVL 22

Expert Comment

by:prairiedog
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now