Solved

how to validate textbox in asp.net

Posted on 2008-10-21
3
2,799 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
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 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