Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

regex 24 hour time validation regex

regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
Comment

How to validate time in 24-hour format using Regular Expression

// Java program to validate the time in
// 24-hour format using Regular Expression.
 
import java.util.regex.*;
 
class GFG {
 
    // Function to validate the time in 24-hour format
    public static boolean isValidTime(String time)
    {
 
        // Regex to check valid time in 24-hour format.
        String regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the time is empty
        // return false
        if (time == null) {
            return false;
        }
 
        // Pattern class contains matcher() method
        // to find matching between given time
        // and regular expression.
        Matcher m = p.matcher(time);
 
        // Return if the time
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "13:05";
        System.out.println(str1 + ": "
                           + isValidTime(str1));
 
        // Test Case 2:
        String str2 = "02:15";
        System.out.println(str2 + ": "
                           + isValidTime(str2));
 
        // Test Case 3:
        String str3 = "24:00";
        System.out.println(str3 + ": "
                           + isValidTime(str3));
 
        // Test Case 4:
        String str4 = "10:60";
        System.out.println(str4 + ": "
                           + isValidTime(str4));
 
        // Test Case 5:
        String str5 = "10:15 PM";
        System.out.println(str5 + ": "
                           + isValidTime(str5));
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: ref schemas mongoose error 
Javascript :: enable native bracket matching vs cide 
Javascript :: jquery prop checked 
Javascript :: js document ready 
Javascript :: twitter icon in next js 
Javascript :: array to set javascript 
Javascript :: Access-Control-Allow-Origin 
Javascript :: string pop last char js 
Javascript :: html loop through array 
Javascript :: node read csv 
Javascript :: js self executing anonymous function 
Javascript :: js wrap an element 
Javascript :: discord.js empty field 
Javascript :: min object value in array 
Javascript :: javascript clear sembols 
Javascript :: js get local date 
Javascript :: alphabetical order array javascript 
Javascript :: error: Error: Unable to resolve module `react-native-gesture-handler` from `node_modules@react-navigation ativelibmoduleScrollables.js`: react-native-gesture-handler could not be found within the project. 
Javascript :: string to JSONobject android 
Javascript :: Jquery Scroll on div using anchor tag is not Working properly 
Javascript :: redirect window 
Javascript :: jquery redirect to another webpage 
Javascript :: node readFileSync json 
Javascript :: india pincode regex 
Javascript :: javascript remove element 
Javascript :: typescript round 
Javascript :: js remove after character 
Javascript :: load script js 
Javascript :: date methods js 
Javascript :: mysql json get value 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =