Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to validate textbox in asp.net

Posted on 2008-10-21
3
Medium Priority
?
2,823 Views
Last Modified: 2012-05-05
how can i validate a  textbox to accept only date values or textbox to accept only integer values in asp.net
0
Comment
Question by:neil17317
3 Comments
 
LVL 6

Accepted Solution

by:
M3mph15 earned 1500 total points
ID: 22773301
Hi,
For both date and integers you can use a Regular ExpressionValidator.
To Validate a date it would be soemthing like this:
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"  ControlToValidate="txtDate" ValidationExpression="^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$">Date needs to be in the format dd/MM/yyyy</asp:RegularExpressionValidator>
And for intergers it would be
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"  ControlToValidate="txtDate" ValidationExpression="\d{4}">Date needs to be in the format dd/MM/yyyy</asp:RegularExpressionValidator>
the 4 in \d{4} is the length of the integers
Hope this helps
-M3mph15
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22779276
A little note on the integer validation.
\d{4} will take 0000 as a valid integer.
Use this to validate 4-digit integer:
^[1-9]\d{3}$
For any length, use
^[0-9]|{1-9]\d*$
0
 

Author Comment

by:neil17317
ID: 22864018
sorry i have'nt mentioned about the time..i'm currently validating about textbox which should only accept datetime format .. do you have a could for this... thanks.................please help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

824 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