Solved

how to validate textbox in asp.net

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
EF5: Question about Metadata Artifact Processing 4 23
Deploying to Azure 3 39
ASP.NET MVC -Add authentication 2 29
asp.net input html code have it display the content 2 22
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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