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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is it possible to preview ajax call before submitting to the database? 18 59
Groupbox Control ? 2 27
Amazon S3 Images with .Net 3 29
Securing WEBAPI on Azure 2 17
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 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