Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi! All,
I have a ASP textbox, and i accept the date in dd-MMM-yyyy format i.e. 01-Jan-2001,
using AJAX Calendar. How can i validate the input of the textbox so that no endusers can enter any characters or any formats.
I just want to restrict users to enter in the specified format in the textbox, if a user enters some other characters he sould be promted to enter in dd-MMM-yyyy.
Thanks.
Posted

You can either use an asp validator or limit the user to type on your date textbox.

Using validator:

XML
<asp:CompareValidator id="dateValidator" runat="server"
  Type="Date" Operator="DataTypeCheck" ControlToValidate="dateTextbox"
  ErrorMessage="Please enter a valid date.">
</asp:CompareValidator>





Using user input limit :

Since you're using Ajax calendar, check your TargetControlID property and set it to readonly.

Good luck!
 
Share this answer
 
Use a RegularExpressionValidator and set it's ControlToValidateID property to the ID of the TextBox. The ValidationExpression of the RegularExpressionValidator will look something like this:

^(([0-9])|([0-2][0-9])|([3][0-1]))\/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\/\d{4}$


You might also need a ValidationSummary, depending on your scenario.

More info for validation controls in ASP.net can be found here:

http://msdn.microsoft.com/en-us/library/debza5t0.aspx[^]
 
Share this answer
 
Comments
Nischal Bhatt 12-Apr-11 4:51am    
Hey! Jim, I Have already tried this, but no results yet...
jim lahey 12-Apr-11 4:56am    
Have you set CausesValidation on the TextBox?

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.causesvalidation.aspx
BobJanova 12-Apr-11 5:34am    
Remember that the month names might be different for different languages.

If you're doing a server side validation, you might as well just try to parse what they provided as a date. Forcing them to enter it in one format is pointless. Any format validation should be done with JS on the client side – its purpose is to avoid unnecessary postback-reload cycles because you didn't type a date right.
try this LINK
Click[^]
 
Share this answer
 
XML
<asp:TextBox ID="txtGsmNo" Width="70px" runat="server"
                                    MaxLength="7" CssClass="select" Height="16px"></asp:TextBox>


XML
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender4" runat="server" FilterType="Numbers"
                                                            TargetControlID="txtGsmNo">
                                                        </cc1:FilteredTextBoxExtender>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900