Javascript RegEx:验证时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11400822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:03:16  来源:igfitidea点击:

Javascript RegEx: validate time

javascriptjqueryregextime

提问by Sri Reddy

I have a stopwatch variable that can get value in the following format: HH:MM:SSmost of the time. 'HH'can go past 24 hours - basically any valid number.

我有一个秒表变量,可以按以下格式获取值: HH:MM:SS大部分时间。'HH'可以超过 24 小时 - 基本上任何有效数字。

I am not that good with this regex thingy, but was able to write something that is good for most part but fails to validate if the hour is single digit. Ex. below regex pass the time 1:10:09, though it is invalid. valid hour part should be 01.

我对这个正则表达式不太好,但能够写出大部分都很好的东西,但无法验证小时是否是个位数。前任。下面的正则表达式通过时间1:10:09,虽然它是无效的。有效小时部分应该是01.

//var stopWatchTime = '02:10:09'; //Valid
//var stopWatchTime = '00:10:09'; //Valid
//var stopWatchTime = '1234:10:09'; //Valid
//var stopWatchTime = ':10:09'; //Invalid
//var stopWatchTime = 'ax:10:09'; //Invalid
var stopWatchTime = '1:10:09'; //Invalid, should be 01:10:09. 

if(!stopWatchTime .match(/^\d+:\d{2}:\d{2}$/)){
    alert('invalid time- ' + stopWatchTime);
}
else{
    alert('valid time - ' + stopWatchTime);
}

What should I change in the regex to say it should check for any number but should have 2 digits or more in hour part?

我应该在正则表达式中更改什么以表示它应该检查任何数字,但在小时部分应该有 2 位或更多位?

回答by Ghost

/^\d{2,}:\d{2}:\d{2}$/

Change the + to a {2,} to allow it to have two or more characters

将 + 更改为 {2,} 以允许它有两个或更多字符

If you want to see the things that matches and doesn't match you can play with it here http://regexr.com?31fu4

如果你想看到匹配和不匹配的东西,你可以在这里玩http://regexr.com?31fu4

EDIT: Also if you only want to match up to 59 in the hours/minutes part use

编辑:此外,如果您只想在小时/分钟部分中匹配多达 59 个,请使用

^\d{2,}:(?:[0-5]\d):(?:[0-5]\d)$

回答by Ghost

回答by Ashish gupta

// use this function to  validate time in HH:mm formate

function validate_time(time){
    var a=true;

    var time_arr=time.split(":");
    if(time_arr.length!=2){           
        a=false;
    }else{
        if(isNaN(time_arr[0]) || isNaN(time_arr[1])){                
            a=false;
        }
        if(time_arr[0]<24 && time_arr[1]<60)
        {

        } else{
            a=false;
        }         
    }
    return a;

}

回答by Omar Trinidad Chavez Mojarro

Another solution could be:

另一种解决方案可能是:

 $('#input-horaIni, #input-horaFin').blur(function () {
        var validTime = $(this).val().match(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/);
        if (!validTime) {
            $(this).val('').focus().css('background', '#fdd');
        } else {
            $(this).css('background', 'transparent');
        }
    });

This works for me... Based on page: http://www.mkyong.com/regular-expressions

这对我有用......基于页面:http: //www.mkyong.com/regular-expressions

回答by Abhijeet

Here is my answer for this validation!

这是我对此验证的回答!

function checkTimes(val){
val=new String(val);
if(val.length!=5){
    return false;
}
else{
    var hour=val.substring(0,2);
    var min=val.substring(3,5);
   if(val.substring(2,3)!==":") return false;
    if(isNaN(hour)) return false;
    if(isNaN(min)) return false;
    if(parseInt(hour)<24){
        if(parseInt(min)<60){
            return true;
        }
        else return false;

    }else return false;
}
}

You can extend my answer for 8chars...

您可以将我的答案扩展为 8 个字符...