以下是我验证错误时间格式的代码:
$('.allts').delegate(".tf", "focusout", function (e)
{
var curval = this.value;
curval = curval.replace(';', ':');
curval = curval.replace(" ", ':');
curval = curval.replace('.', ':');
//alert(curval);
if (/^\d$/.test(curval))
{
curval = "0" + curval + ":00";
}
if (/^[0-9]:[0-5][0-9]$/.test(curval))
{
curval = "0" + curval;
}
if (curval >= 10 && curval <= 12)
{
curval = curval + ":00";
}
if(curval.length==2&&curval >= 0 && curval <= 12){
curval = curval + ":00";
}
this.value = curval;
if (this.value != "")
{
if (!isValidDate(curval))
{
this.style.background = "#faa";
$('#msg_tformat').html('Please enter a valid time').show();
this.value = "";
}
我的要求是不接受0:00时间格式。如果我给出像0:00这样的时间格式,它应该显示如下错误消息:“请输入有效时间”。任何人都可以建议 在JavaScript中验证这一点的正确方法?
提前致谢。
答案 0 :(得分:0)
添加isValidDate
函数,如下所示:
if( curval === '0:00' || curval === '00:00' ) {
return false;
}