Validate UUID String in Java

Backend Pro

 There are 2 techniques to validate a UUID in Java.

  1. Using UUID.fromString()
  2. Using Regular Expressions

Using UUID.fromString():


This approach is the simplest & easiest way to validate UUID Strings. The UUID class is from java.util package and provides a method to convert the UUID String to UUID Object. This method throws an IllegalArgumentException when the given string is not a valid UUID. 

for example: when the length of provided UUID String is less than 36 characters, it throws IllegalArgumentException with the message "UUID string too large".

This method also looks from dashes (-) in the provided UUID String and if they are not in the correct positions, an IllegalArgumentException with the message "Invalid UUID string" is thrown. 

The only problem with this approach is, UUID.fromString() doesn't fail for cases such as "0-0-0-0-0". In this case, it returns as "00000000-0000-0000-0000-000000000000". We have to manually fix this by comparing the length to mitigate this. If the String length is less than 36 chars, throw an exception or return as false. 
  
package com.thebackendpro.uuid.validation;

import java.util.UUID;

public class UUIDValidator {

    public static void main(String[] args) {
        // Test 1
        boolean isValid = isValidUUID("00000000-0000-0000-0000-000000000000");
        if (isValid) {
            System.out.println("00000000-0000-0000-0000-000000000000 is a valid UUID");
        } else {
            System.out.println("00000000-0000-0000-0000-000000000000 is not a valid UUID");
        }

        // Test 2
        isValid = isValidUUID("0000000-000-00-000000");
        if (isValid) {
            System.out.println("0000000-000-00-000000 is a valid UUID");
        } else {
            System.out.println("0000000-000-00-000000 is not a valid UUID");
        }

        // Test 3
        isValid = isValidUUID("0-0-0-0-0");
        if (isValid) {
            System.out.println("0-0-0-0-0 is a valid UUID");
        } else {
            System.out.println("0-0-0-0-0 is not a valid UUID");
        }
    }

    private static boolean isValidUUID(String uuid) {
        try {
            return UUID.fromString(uuid) != null;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }
}

Output:

00000000-0000-0000-0000-000000000000 is a valid UUID
0000000-000-00-000000 is not a valid UUID
0-0-0-0-0 is a valid UUID

  

Using Regular Expressions:


Regular expressions provide a way to validate UUID strings. This is the best approach as it returns false even for the UUID String "0-0-0-0-0". 

The expression to validate UUID is "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$". 

The snippet is as follows


package com.thebackendpro.uuid.validation;

import java.util.regex.Pattern;

public class UUIDValidatorRegEx {

    private static Pattern REG_EX_UUID = Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");

    public static void main(String[] args) {
        // Test 1
        boolean isValid = isValidUUID("00000000-0000-0000-0000-000000000000");
        if (isValid) {
            System.out.println("00000000-0000-0000-0000-000000000000 is a valid UUID");
        } else {
            System.out.println("00000000-0000-0000-0000-000000000000 is not a valid UUID");
        }

        // Test 2
        isValid = isValidUUID("0000000-000-00-000000");
        if (isValid) {
            System.out.println("0000000-000-00-000000 is a valid UUID");
        } else {
            System.out.println("0000000-000-00-000000 is not a valid UUID");
        }

        // Test 3
        isValid = isValidUUID("0-0-0-0-0");
        if (isValid) {
            System.out.println("0-0-0-0-0 is a valid UUID");
        } else {
            System.out.println("0-0-0-0-0 is not a valid UUID");
        }
    }

    private static boolean isValidUUID(String uuid) {
        return REG_EX_UUID.matcher(uuid).matches();
    }
}



00000000-0000-0000-0000-000000000000 is a valid UUID
0000000-000-00-000000 is not a valid UUID
0-0-0-0-0 is not a valid UUID

Conclusion:


These are the 2 ways used to validate UUID String in Java. Look for source code at TheBackendPro



Tags

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !