rodrigosantosbr

Validate hour to be between 00:00 and 23:59

Jan 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

Site:
https://regex101.com/
https://www.jdoodle.com/python3-programming-online
https://playcode.io/online-javascript-editor


Validate hour to be between 00:00 and 23:59

([0-1][0-9]|2[0-3]):[0-5][0-9]

Javascript

var re = /^([0-1][0-9]|2[0-3]):[0-5][0-9]$/;
var term1 = "00:00";
var term2 = "23:59";
var term3 = "23:60";
var term4 = "24:00";

if (re.exec(term1)) {
    console.log(term1+": Valid");
}
else {
    console.log(term1+": Invalid");
}

if (re.exec(term2)) {
    console.log(term2+": Valid");
}
else {
    console.log(term2+": Invalid");
}

if (re.exec(term3)) {
    console.log(term3+": Valid");
}
else {
    console.log(term3+": Invalid");
}

if (re.exec(term4)) {
    console.log(term4+": Valid");
}
else {
    console.log(term4+": Invalid");
}

Python

import re
patt = r'([0-1][0-9]|2[0-3]):[0-5][0-9]'
term1 = "00:00"
match1 = re.search(patt,term1)
term2 = "23:60";
match2 = re.search(patt,term2)

if match1:
    print("Valid: {}".format(match1.group()))
else:
    print("Invalid: {}".format(term1))

if match2:
    print("Valid: {}".format(match2.group()))
else:
    print("Invalid: {}".format(term2))
Add Comment
Please, Sign In to add comment