我有一个用于输入date.Textbox的文本框应该只允许dd / MM / yyyy格式。如何使用javascript检查它
答案 0 :(得分:2)
如果您使用的是WebForms,请使用CompareValidator:
<asp:CompareValidator runat="server" ControlToValidate="txtInput" Type="Date"
Operator="DataTypeCheck" ErrorMessage="That's not a valid date!" />
答案 1 :(得分:1)
您正在使用dd / mm / yyyy来验证日期 - 您可以使用JavaScript日期对象来验证日期,但在这种情况下它更棘手:
var o = document.form1.date1; // o is a reference to the textbox
var r = /^(\d+)\/(\d+)\/(\d+)$/.exec( o.value ); // extract day, month and year
if ( ! r )
{
// invalid date -- pattern matching failed
}
var d = parseInt( r[ 1 ], 10 );
var m = parseInt( r[ 2 ], 10 );
var y = parseInt( r[ 3 ], 10 );
var c = new Date( y, m - 1, d ); // month is zero based but year and day are not
if ( isNaN( c ) )
{
// invalid date -- javascript could not make a date out of the three numbers
}