验证日期不得大于今天的日期? [英] Validate that date must not be greater than todays date ?
本文介绍了验证日期不得大于今天的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< asp:TextBox ID =txtStartDateCssClass =Form_InputFieldrunat =server>
this是我的文本框..如何验证在此输入的值不得大于今天的日期...
解决方案
对于系统.DateTime
,定义了运算符'< ='等。
要从字符串中获取值,请使用以下方法之一Parse
,ParseExact
,TryParse
或TryParseExact
,具体取决于所需的格式或文化。请参阅:
http://msdn.microsoft.com/en -us / library / system.datetime.aspx [ ^ ]。
但是,使用文本框编辑日期/时间并不是一个好主意。使用一些DateTimePicker
会好得多。请参阅我以前的回答: TextBox允许仅使用(HH:mm tt)格式使用asp.net [ ^ ]。
-SA
请参阅这些链接以验证日期格式。
http://stackoverflow.com/questions/3005425/how-检查或验证文本框输入日期是在dd-mm-yyyy格式 [ ^ ]
之后添加以下代码来验证日期。
DateTime currentdate = Convert.ToDateTime(DateTime.Now.ToString( dd / MM / yyyy 跨度>));
DateTime ToDate = DateTime.ParseExact(e.Value, dd / MM / yyyy ,CultureInfo.InvariantCulture);
if (ToDate > currentdate)
{
lblShowDateError。 Visible = true ;
lblShowDateError.Text = 结束日期不应早于当前日期。;
return ;
}
else
{
lblShowDateError.Text = ;
}
如果你使用calenderExtenderextender控件..在脚本标签处输入这样的代码...你在calenderextender控件中的这个方法添加这个适当性...
OnClientDateSelectionChanged =checkDate
< pre lang = xml > & lt; script type =& ; QUOT;文本/ JavaScript的&安培; QUOT;&安培; GT;
function checkDate(sender,args){
if(sender._selectedDate & gt; new Date()){
提醒(& quot;你不能选择未来的日期!& quot;);
sender._selectedDate = new Date();
//将日期设置回当前日期
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}
} < / pre >
<asp:TextBox ID="txtStartDate" CssClass="Form_InputField" runat="server">
this is my text box .. how can i validate that value entered in this must not be greater than todays date ...
解决方案
ForSystem.DateTime
, operator '<=' is defined, among others.
To get a value from a string, use one of the methodsParse
,ParseExact
,TryParse
orTryParseExact
depending on required format or culture. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].
However, using a text box to edit date/time is not really a good idea. It's much better to use someDateTimePicker
. Please see my past answer: TextBox allow only (HH:mm tt) format using asp.net[^].
—SA
Refer these links to validate the date format.
http://stackoverflow.com/questions/3005425/how-to-check-or-validate-the-textbox-entered-date-is-in-dd-mm-yyyy-format[^]
after this add the below code to validate the date.
DateTime currentdate = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy")); DateTime ToDate = DateTime.ParseExact(e.Value,"dd/MM/yyyy", CultureInfo.InvariantCulture); if (ToDate > currentdate) { lblShowDateError.Visible = true; lblShowDateError.Text = "End Date should not be earlier than the current date."; return; } else { lblShowDateError.Text = ""; }
If u use calenderExtenderextender control.. enter your code like this at script tag...u cal this method in calenderextender control add this properity also...
OnClientDateSelectionChanged="checkDate" <pre lang="xml"><script type="text/javascript"> function checkDate(sender, args) { if (sender._selectedDate > new Date()) { alert("You cannot select a future date!"); sender._selectedDate = new Date(); // set the date back to the current date sender._textbox.set_Value(sender._selectedDate.format(sender._format)) } }</pre>
这篇关于验证日期不得大于今天的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文