How to Test Valid UUID or GUID in javascript

This tutorial explains how to check given UUID or GUID is a valid string or not. UUID string is an universally unique identifier (UUID) format, that is used to identify objects or entities. It follows RFC4122 specification.

How to check if UUID or GUID is valid in javascript

Valid UUID satisfies the below conditions

  • UUID contains 32 characters total, contains a hexa string, separated by a hyphen
  • Every Character is a hexadecimal
  • String is the 8-4-4-4-12 characters format, separated with a hyphen

Plain Javascript condition check for UUID example,

In this example, first replace the hyphen with empty spaces, next, check total length is 32, and whether each character is hexa decimal or not.

// checks given character is Hexa decimal character
const isHexaCharacter = (character) => "0123456789abcdef".includes(character.toLowerCase());
// checks given string is valid UUID
const isValidUUID = (input) => {
  //Replace the hyphen with a space
  input = input.replaceAll("-", " ");
  // check input length is 32, and each character is hexa decimal
  return input.length === 32 && [...input].every(isHexaCharacter);
};

console.log(isValidUUID("abc"));  // false
console.log(isValidUUID("8336b1c6-59e7-4ac7-a0f6-0a80a38ef6a6"));  // true
console.log(isValidUUID("KLM"));   // false
console.log(isValidUUID(""));   // false

Another way, you check using regular expressions using the 8-4-4-4-12 string.

Check valid UUID in Nodejs

Node has a uuid package which provides multiple functions to generate different versions of UUID, and also a validate function to check whether a given uuid is valid or not.

The validate function checks for given uuid is valid as per all versions.

const { v4: uuidv4, validate } = require("uuid");
const uuidString = "8336b1c6-59e7-4ac7-a0f6-0a80a38ef6a6";
console.log(validate(uuidString)); // true
console.log(validate("1abc")); // false
console.log(validate(" ")); // false

Conclusion

Multiple options are listed above, choose based on your needs.

title: “How to Test Valid UUID or GUID in javascript” date: 2024-03-10T00:00:00.000-00:00 draft: false image: javascript/javascript-logo.png tags: [javascript] url: javascript-check-valid-uuid-example description: check if UUID or GUID is valid or not in nodejs and plain javascript example Checks for hexa decimal character of 32 and format of 8,4,4,4,12 characters separated by a hyphen

This tutorial explains how to check given UUID or GUID is a valid string or not. UUID string is an universally unique identifier (UUID) format, that is used to identify objects or entities. It follows RFC4122 specification.

How to check if UUID or GUID is valid in javascript

Valid UUID satisfies the below conditions

  • UUID contains 32 characters total, contains a hexa string, separated by a hyphen
  • Every Character is a hexadecimal
  • String is the 8-4-4-4-12 characters format, separated with a hyphen

Plain Javascript condition check for UUID example,

In this example, first replace the hyphen with empty spaces, next, check total length is 32, and whether each character is hexa decimal or not.

// checks given character is Hexa decimal character
const isHexaCharacter = (character) => "0123456789abcdef".includes(character.toLowerCase());
// checks given string is valid UUID
const isValidUUID = (input) => {
  //Replace the hyphen with a space
  input = input.replaceAll("-", " ");
  // check input length is 32, and each character is hexa decimal
  return input.length === 32 && [...input].every(isHexaCharacter);
};

console.log(isValidUUID("abc"));  // false
console.log(isValidUUID("8336b1c6-59e7-4ac7-a0f6-0a80a38ef6a6"));  // true
console.log(isValidUUID("KLM"));   // false
console.log(isValidUUID(""));   // false

Another way, you check using regular expressions using the 8-4-4-4-12 string.

Check valid UUID in Nodejs

Node has a uuid package which provides multiple functions to generate different versions of UUID, and also a validate function to check whether a given uuid is valid or not.

The validate function checks for given uuid is valid as per all versions.

const { v4: uuidv4, validate } = require("uuid");
const uuidString = "8336b1c6-59e7-4ac7-a0f6-0a80a38ef6a6";
console.log(validate(uuidString)); // true
console.log(validate("1abc")); // false
console.log(validate(" ")); // false

Conclusion

Multiple options are listed above, choose based on your needs.