What is is-valid-domain?
The is-valid-domain npm package is used to validate domain names. It checks if a given string is a valid domain name according to the rules specified in various domain name standards.
What are is-valid-domain's main functionalities?
Basic Domain Validation
This feature allows you to check if a given string is a valid domain name. The function returns true if the domain is valid and false otherwise.
const isValidDomain = require('is-valid-domain');
console.log(isValidDomain('example.com')); // true
console.log(isValidDomain('invalid_domain')); // false
Subdomain Validation
This feature allows you to validate subdomains. It ensures that subdomains follow the correct format and rules.
const isValidDomain = require('is-valid-domain');
console.log(isValidDomain('sub.example.com')); // true
console.log(isValidDomain('sub..example.com')); // false
Top-Level Domain (TLD) Validation
This feature checks if the domain has a valid top-level domain (TLD). It ensures that the TLD is recognized and valid.
const isValidDomain = require('is-valid-domain');
console.log(isValidDomain('example.xyz')); // true
console.log(isValidDomain('example.invalidtld')); // false
Other packages similar to is-valid-domain
validator
The validator package is a library of string validators and sanitizers. It includes a function `isFQDN` that can be used to validate domain names. Compared to is-valid-domain, validator offers a broader range of validation functions beyond just domain validation.
valid-url
The valid-url package provides functions to validate URLs and URIs. While it can validate domain names as part of a URL, it is more focused on the overall URL structure rather than just the domain. It is less specialized than is-valid-domain for domain-specific validation.
is-url
The is-url package is a simple utility to check if a string is a valid URL. It includes domain validation as part of its URL validation process. However, it is not as focused on domain-specific rules as is-valid-domain.
is-valid-domain
Validate domain name in JavaScript
Demo
https://lab.miguelmota.com/is-valid-domain
Install
npm install is-valid-domain
Usage
const isValidDomain = require('is-valid-domain')
isValidDomain('example.com')
isValidDomain('foo.example.com')
isValidDomain('bar.foo.example.com')
isValidDomain('exa-mple.co.uk')
isValidDomain('xn--80ak6aa92e.com')
isValidDomain('_dnslink.ipfs.io')
isValidDomain('exa_mple.com')
isValidDomain('-example.co.uk')
isValidDomain('example')
isValidDomain('ex*mple.com')
isValidDomain('*.example.com')
isValidDomain('*.com')
isValidDomain(3434)
isValidDomain('foo.example.com', {subdomain: true})
isValidDomain('foo.example.com', {subdomain: false})
isValidDomain('*.example.com', {wildcard: false})
isValidDomain('*.example.com', {wildcard: true})
isValidDomain('*.example.com', {subdomain: false, wildcard: true})
isValidDomain('はじめよう.みんな')
isValidDomain('はじめよう.みんな', {allowUnicode: true})
isValidDomain('ai.')
isValidDomain('ai.', {topLevel: true})
view more examples
Test
npm test
Contributing
Adding new domains:
- Add second level domain to
data/second_level_domains.csv
- Run
npm run generate
to generate JSON map file - Run
npm test
FAQ
License
MIT