Domain Name Validation Using RegEx in JavaScript

Emad Uddin
2 min readNov 25, 2023

--

Domain Validation

Introduction:

Validating user inputs is an essential part of web development to guarantee data security and integrity. Developers frequently have to create solutions to make sure that input follows expected patterns while processing URLs, particularly domain validation. We’ll look at and talk about a JavaScript method for domain validation in this blog article.

The Domain Validation Function:

First, let’s look at the JavaScript function that handles domain validation. This function accepts a URL as input and outputs a boolean value that indicates if the supplied URL is a legitimate domain or not.

// Domain Validation
domainValidation(url) {
const urlRegex = /^(((http|https):\/\/|)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?)$/;
if (urlRegex.test(url)) {
return true;
} else {
return false;
}
};

Breaking Down the Regular Expression:

Now, let’s break down the regular expression used in the function:

  1. ^: Asserts the start of the string.
  2. (((http|https):\/\/|)?: Captures an optional “http://” or “https://” at the beginning of the URL.
  3. [a-z0–9]+: Matches one or more alphanumeric characters for the domain name.
  4. ([\-\.]{1}[a-z0–9]+)*: Allows for hyphens or dots followed by more alphanumeric characters, allowing for subdomains.
  5. \.[a-z]{2,6}: Matches the top-level domain (TLD) with a length between 2 and 6 characters.
  6. (:[0–9]{1,5})?: Captures an optional port number following a colon.
  7. (\/.*)?$: Captures an optional path following a forward slash, ensuring the end of the string.

More RegEx For Domain and Url Validation:

Option 1: URL and Domain Validation Inside any Text

const urlRegex = /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/g;

This Regular expression can detect the perfect domain and URL from any long text.

Option 2: URL Validation with Secure Protocol (https://)

 const urlHttpsRegex = /^(http(s):\/\/.)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$/g;

This regular expression can detect url with https:// protocol. If you want to check the URL with https:// protocol, you should try this one.

Option 3: Simplified Domain Validation

const domainRegex = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/i;

`domainRegex` can detect the only valid domain in the input text. If you want to check the domain only, you can use this RegEx.

Option 4: Comprehensive Domain and URL Validation Regex

const domainAndUrlRegEx = /^(((http|https):\/\/|)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?)$/;

`domainAndUrlRegEx` can detect the valid domain and URL. A more comprehensive regex that includes protocol, domain, port, and path components.

Conclusion:

Building strong web applications requires domain validation, so developers should find it useful to have a dependable tool for the job. This blog post’s JavaScript function uses a regular expression to determine whether a given URL complies with expected domain patterns. Please feel free to modify or add this feature to your projects to improve URL input security and dependability. Happy coding!

--

--