如何验证ASP:Textbox仅接受日期格式? [英] How to Validate ASP:Textbox to accept only date formats?
问题描述
嗨!全部,
我有一个ASP文本框,并且我接受dd-MMM-yyyy格式的日期,即2001年1月1日,
使用 AJAX 日历.如何验证文本框的输入,以便最终用户不能输入任何字符或任何格式.
我只想限制用户以指定的格式在文本框中输入内容,如果用户输入了其他字符,则提示他输入dd-MMM-yyyy.
谢谢.
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.
推荐答案
您可以使用asp验证程序,也可以限制用户在日期文本框中键入内容.
使用验证器:
You can either use an asp validator or limit the user to type on your date textbox.
Using validator:
<asp:CompareValidator id="dateValidator" runat="server"
Type="Date" Operator="DataTypeCheck" ControlToValidate="dateTextbox"
ErrorMessage="Please enter a valid date.">
</asp:CompareValidator>
使用用户输入限制:
由于您使用的是Ajax日历,因此请检查TargetControlID属性并将其设置为只读.
祝你好运!
Using user input limit :
Since you''re using Ajax calendar, check your TargetControlID property and set it to readonly.
Good luck!
使用RegularExpressionValidator并将其ControlToValidateID属性设置为TextBox的ID. RegularExpressionValidator的ValidationExpression看起来像这样:
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}
根据您的情况,您可能还需要一个ValidationSummary.
有关ASP.net中验证控件的更多信息,请参见:
http://msdn.microsoft.com/en-us/library/debza5t0.aspx [ ^ ]
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[^]
这篇关于如何验证ASP:Textbox仅接受日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!