Solved

how to validate textbox in asp.net

Posted on 2008-10-21
3
2,789 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

13 Experts available now in Live!

Get 1:1 Help Now