<script language = javascript> /// Validate data type /// VersionHistroy: Auther Date Version Desc. /// Ding CG 03.09.05 1.0 Init.Version 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 = "a2~!@#$%^&*()_+,./;':"; }
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(); } </script>报错但是允许焦点移动: <asp:textbox id="TextBox1" runat="server" onChange="ValidateDataType('Date')></asp:textbox> 报错并保留焦点: <asp:textbox id="TextBox1" runat="server" onBlur="ValidateDataType('Date')></asp:textbox>试试看,^_^
/// Validate data type
/// VersionHistroy: Auther Date Version Desc.
/// Ding CG 03.09.05 1.0 Init.Version
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 = "a2~!@#$%^&*()_+,./;':";
}
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();
}
</script>报错但是允许焦点移动:
<asp:textbox id="TextBox1" runat="server" onChange="ValidateDataType('Date')></asp:textbox>
报错并保留焦点:
<asp:textbox id="TextBox1" runat="server" onBlur="ValidateDataType('Date')></asp:textbox>试试看,^_^
{}
catch
{
弹出js对话框
}
这个也可一