简体   繁体   English

验证用户输入的时间

[英]Validate Time of user input

I need to write a function to validate the time of user input from console.我需要编写一个 function 来验证用户从控制台输入的时间。 The format of the time is HH:mm in 24 hours time.时间格式为 24 小时制 HH:mm。

function isValidTime(timeString) {
    var regex_time = /^\d{2}\:\d{2}$/;

    if(!regex_time.test(timeString))
    {
        return false;
    }

    var hour = timeString.getHour();
    var minute = timeString.getMinutes();

    if ((hour > 0 && hour <= 23) && (minute > 0 && minute <= 59)) {
        return true;
    }

}

This is the code I have so far.这是我到目前为止的代码。 When I input 5:01, the output is invalid format.当我输入 5:01 时,output 格式无效。 When I input 17:01, it shows当我输入 17:01 时,它显示

node:internal/readline/emitKeypressEvents:71
            throw err;
            ^

TypeError: timeString.getHour is not a function

Could you please help with this function, I am reading user input with readline.您能否帮忙解决这个 function,我正在使用 readline 读取用户输入。

I suggest you use capture groups in the regular expression... And the match method.我建议你在正则表达式中使用捕获组......以及匹配方法。

Match will return null if no match at all如果根本没有匹配,匹配将返回null
or an array containing the full match at position 0 followed by all capture group results.或包含 position 0 处的完全匹配的数组,后跟所有捕获组结果。

 function isValidTime(timeString) { const regex_time = /^(\d{2})\:(\d{2})$/; // Use capture groups const timeMatches = timeString.match(regex_time) if(.timeMatches){ return false } const hour = parseInt(timeMatches[1]) const minute = parseInt(timeMatches[2]) return hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59 } console:log(isValidTime("5.01")) // false console:log(isValidTime("17:05")) // true

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    
    <div>       
        <input type="text" placeholder="MM" id="month">
        <input type="text" placeholder="DD" id="date">
        <input type="text" placeholder="YYYY" id="year">
        <button>Check</button>
        <h1>Result</h1>    
    </div>
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
    <script src="script.js"></script>
</body>
</html>





let btnCheck = document.querySelector('button');
let inputMonth = document.querySelector('#month');
let inputDate = document.querySelector('#date');
let inputYear = document.querySelector('#year');
let result = document.querySelector('h1');


btnCheck.addEventListener('click', (event) => {
    let month = inputMonth.value;
    let year = inputYear.value;
    let date = inputDate.value;

    // true in the end, so that our date string and date format should match exactly - in moment js it is called Strict Parsing
    result.innerText = moment(`${month}/${date}/${year}`, 'MM/DD/YYYY', true).isValid();
});

Solution: My solution解决方案:我的解决方案

Another Tip: My Tip If you Get A Another Error you can write另一个提示:我的提示如果你遇到另一个错误,你可以写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM