判断输入字符串是否是有效时间
专案需要,一个TextBox要存储时间值。由于asp.net的Validation的验证工具的asp:CompareValidator只能验到日期。验证日期可以参考:http://www.cnblogs.com/insus/archive/2012/03/24/2415171.html
如何判断用户在TextBox输入的字符串就是一个有效的时间呢? Insus.NET解决方法并没有使用正则表达式,也可以实现到相同验证效果,没有警告提示,说明输入的字符串是一个有效的时间字符串:
.aspx,放置一个TextBox, 写OnTextChanged事件,还要设置AutoPostack=“true”属性。
<asp:TextBox ID="TextBoxSendTime" runat="server" CssClass="textbox" Width="70" AutoPostBack="true" OnTextChanged="TextBoxSendTime_TextChanged"></asp:TextBox>
.aspx.cs:
protected void TextBoxSendTime_TextChanged(object sender, EventArgs e)
{
var tb = sender as TextBox;
//ConverData类别,可以参考:http://www.cnblogs.com/insus/articles/1424094.html
if (ConvertData.ToDateTime(DateTime.Today.ToString("yyyy-MM-dd") + " " + tb.Text.Trim()) == null)
{
//以下类别,参考:http://www.cnblogs.com/insus/articles/1341703.html
InsusJsUtility objInsusJsUtility = new InsusJsUtility();
objInsusJsUtility.JsAlert("输入字符串不是有效时间。");
return;
}
}
{
var tb = sender as TextBox;
//ConverData类别,可以参考:http://www.cnblogs.com/insus/articles/1424094.html
if (ConvertData.ToDateTime(DateTime.Today.ToString("yyyy-MM-dd") + " " + tb.Text.Trim()) == null)
{
//以下类别,参考:http://www.cnblogs.com/insus/articles/1341703.html
InsusJsUtility objInsusJsUtility = new InsusJsUtility();
objInsusJsUtility.JsAlert("输入字符串不是有效时间。");
return;
}
}