The isTime
function checks if the input value represents a valid time in the format "hh:mm" or "hh:mm AM/PM" or "hh:mm:ss" or "hh:mm:ss AM/PM". It returns true
if the value matches any of these formats, and false
otherwise.
The function can be imported using ES6 syntax from the "multiform-validator" package:
import { isPostalCode } from 'multiform-validator';
Alternatively, you can import the function using CommonJS syntax with require
(Node.js):
const { isPostalCode } = require('multiform-validator');
The function takes one parameter, which must be a string representing the time to be checked.
time
(string) - The time to be checked for validity. It must be in one of the supported formats: "hh:mm", "hh:mm AM/PM", "hh:mm:ss", or "hh:mm:ss AM/PM".// Example 1 - Valid time formats
const result1 = isTime('12:34'); // true
console.log(result1);
const result2 = isTime('12:34 AM'); // true
console.log(result2);
const result3 = isTime('23:59:59'); // true
console.log(result3);
const result4 = isTime('12:34:56 PM'); // true
console.log(result4);
const result5 = isTime('12:34:56 AM'); // true
console.log(result5);
// Example 2 - Invalid time formats
const result6 = isTime('12:34:56XM'); // false (invalid format)
console.log(result6);
const result7 = isTime('25:00'); // false (invalid hour)
console.log(result7);
const result8 = isTime('23:60'); // false (invalid minute)
console.log(result8);
const result9 = isTime('23:59:61'); // false (invalid second)
console.log(result9);
The function uses a regular expression to check if the input time string matches any of the supported time formats. It returns true
if the input value matches any of the supported formats, and false
otherwise.