Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

NodeJS UUID Validate

To validate UUID (GUID) in NodeJS, we can use below options:

  1. UUID NPM Package
  2. We can write our own custom regular expressions to validate.

1. UUID NPM Package

To use UUID package, we can use below command to include in our project.

NodeJS Install UUID Package

To validate UUID in NodeJS, we can use validate method of UUID package. Below is the sample code.


const uuid = require('uuid');

const uniqueId = "3112b3db-c1ef-4cc2-994b-306d75834277";

const isValid = uuid.validate(uniqueId); //return true as Valid, false as Invalid

In the first line, we are importing UUID NPM package. In second line, we are declaring a custom uuid number. In the third line, we are validating UUID with the help of validate method. This method returns “true”, if the uuid is valid else returns false.

With the help of UUID package, we can check the version of the UUID. A UUID can have different versions from 1 to 5. To check the version of UUID, we can use version() method of UUID package. Below is the code.


const uuidVersion = uuid.version(uniqueId);

2. Custom regular expressions to Validate UUID

We can write different regular expressions to validate different versions of UUID. Below are the options:


v1: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1][0-9a-fA-F]{3}-[89AB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/i

v2: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[2][0-9a-fA-F]{3}-[89AB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/i

v3: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[3][0-9a-fA-F]{3}-[89AB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/i

v4: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[4][0-9a-fA-F]{3}-[89AB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/i

v5: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[5][0-9a-fA-F]{3}-[89AB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/i

To validate, use test method as shown below. we are validating version 4 uuid.


const isValid = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[4][0-9a-fA-F]{3}-[89AB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/i.test('3112b3db-c1ef-4cc2-994b-306d75834277')

To use a common regular expression to validate any version of UUID. Use below regular expression.

v1-v5: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89AB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/i

In the below example, we are validating version 1 uuid.


const isValid = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89AB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/i.test('3112b3db-c1ef-1cc2-994b-306d75834277')

Summary

To validate UUID in NodeJS, we can either use NPM UUID package or use custom regular expressions. We can validate any version of UUID easily.