方法一:
//日期格式yyyy-mm-dd
<asp:rangevalidator id="vldRngDate" runat="server" CssClass="main" ErrorMessage="日期必须在1900~2500之间" ControlToValidate="txtDate" Display="None" MinimumValue="1900-01-01" MaximumValue="2500-12-31" Type="Date"></asp:rangevalidator>
方法二:
//验证是否为空
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" Display="Dynamic" ControlToValidate="TextBox"></asp:RequiredFieldValidator>
//工具箱--->选择CompareValidator-->ControlToValidate为需要验证的输入框-->Operatoe为你要验证的方式-->Type为你需要验证的类型
//验证是否是日期格式的
<asp:CompareValidator id="CompareValidator1" runat="server" ErrorMessage="CompareValidator" Display="Dynamic" ControlToValidate="TextBox" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>
方法三:
//正则表达式
//出错警告并允许焦点离开:
<asp:TextBox>< id="textbox1" onChange="ValidateDataType( ‘Date‘ ) " runat=server>
</asp:TextBox>
//出错警告并不允许焦点离开:
<asp:TextBox>< id="textbox1" onBlur="ValidateDataType( ‘Date‘ ) " runat=server>
</asp:TextBox>
<JavaScript>
function ValidateDataType( strDataType )
{
var Reg ;
var sExample
if ( strDataType.match( "Curr") )
{
Reg = /^(\+|-)?\d+(,\d\d\d)*(\.\d*)?$/g ;//Deal as double
sExample = "\n123 +123 -123\n23456.78 -23456.78\n23,456.78 -23,456.78\n0.12 -0.12\n.12 -.12\n0 +0 -0 ";
}
else if ( strDataType.match( "Date") )
{
Reg = /^([1-2]\d{3})(-|\.)(0?[1-9]|10|11|12)(-|\.)(([0-2]?[1-9])|10|20|30|31)$/g ;
sExample = "\n2003-08-24 2003.08.24\n1998-12-1 1998.12.1";
}
else if ( strDataType.match( "Doub") )
{
Reg = /^(\+|-)?\d+(,\d\d\d)*(\.\d*)?$/g ;
sExample = "\n123 +123 -123\n23456.78 -23456.78\n23,456.78 -23,456.78\n0.12 -0.12\n.12 -.12\n0 +0 -0 ";
}
else if ( strDataType.match( "Int") )
{
Reg = /^(\+|-)?\d+(,\d\d\d)*$/g ;
sExample = "\n23456 +23456 -23456\n23,456 -23,456\n0 +0 -0";
}
else
{
Reg = /^.*$/g ;
sExample = "[email protected]#$%^&*()_+,./;‘:";
}
var e = window.event.srcElement;
var str = e.value.trim();
if ( str == "" ) return;
if ( Reg.exec( str )) return;
alert("Error : The value [ " + str + " ] does not fit with the data type [ " + strDataType + " ]."
+ " \n\nYou may follow these formats : " + sExample );
e.focus();
}
</JavaScript>