Asp.net 验证作为单独字段输入的日期/时间部分
环境:ASP.NET3.5,VisualStudio2008 我有3个文本框Asp.net 验证作为单独字段输入的日期/时间部分,asp.net,validation,Asp.net,Validation,环境:ASP.NET3.5,VisualStudio2008 我有3个文本框 TextBox1.Text = "is a date value"; TextBox2.Text = "is hour of a datetime"; TextBox3.Text = "is minutes of a datetime"; (不要问为什么:)这是其中之一……事实就是如此) 是否有一种方法可以使用单个asp.net验证程序控件来检查(客户端和服务器端)所有3个文本框的组合是否为有效的日期时间 此外,在我
TextBox1.Text = "is a date value";
TextBox2.Text = "is hour of a datetime";
TextBox3.Text = "is minutes of a datetime";
(不要问为什么:)这是其中之一……事实就是如此)
是否有一种方法可以使用单个asp.net验证程序控件来检查(客户端和服务器端)所有3个文本框的组合是否为有效的日期时间
此外,在我看来,似乎可以有各种可能性(例如,只需在其中一个文本框中输入内容,有效日期但无效时间,等等)
如果不可能,服务器端检查会是什么样子
例如,到目前为止,我所掌握的是:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
DateTime dateOutput = default(DateTime);
DateTime.TryParse(TextBox1.Text, out dateOutput);
if (dateOutput != DateTime.MinValue)
{
string dateString = string.Empty;
if (TextBox2.Text == string.Empty && TextBox3.Text == string.Empty)
dateString = dateString = string.Format("{0} {1}:{2}", TextBox1.Text, DateTime.Now.Hour, DateTime.Now.Minute);
DateTime.TryParse(dateString, out dateOutput);
}
args.IsValid = (dateOutput != DateTime.MinValue);
}
我是否遗漏了什么或可以改进?必须通过客户端脚本和服务器脚本验证用户输入。您可以选择customValidator
。您还可以通过处理
函数validateMyForm(){
//编写代码以验证用户输入。
如果(!成功)
返回false;
返回true;
}
...
必须通过客户端脚本和服务器脚本验证用户输入。您可以选择customValidator
。您还可以通过处理
函数validateMyForm(){
//编写代码以验证用户输入。
如果(!成功)
返回false;
返回true;
}
...
很抱歉,我不清楚,但我更感兴趣的是验证日期案例的最佳方式(特别是服务器端例程)。很抱歉,我不清楚,但我更感兴趣的是验证日期案例的最佳方式(特别是服务器端例程)。
<script type="text/javascript">
function validateMyForm(){
//write code to validate user input.
if(!succeed)
return false;
return true;
}
</script>
<form id="form1" runat="server" onsubmit="return validateMyForm()">
...
</form>