RSSAmplifier

(untitled) · Jan 4, 2015

Regex to validate domain name

0
Sign in to vote or save

This site does not allow itself to be embedded. You can still read it on the original site — the toolbar below keeps your place in the directory.

Using a regular expression to check if it is a valid domain. function isValidDomain(v) { if ( ! v) return false ; var re = /^(?!:\/\/)([a-zA-Z0-9-]+\.){0,5}[a-zA-Z0-9-][a-zA-Z0-9-]+\.[a-zA-Z]{2,64}?$/gi ; return re.test(v); } Usage isValidDomain( 'example.com' ) // true isValidDomain( 'foo.example.com' ) // true isValidDomain( 'bar.foo.example.com' ) // true isValidDomain( 'exa-mple.co.uk' ) //…

Using a regular expression to check if it is a valid domain.

function isValidDomain(v) {
 if (!v) return false;
 var re = /^(?!:\/\/)([a-zA-Z0-9-]+\.){0,5}[a-zA-Z0-9-][a-zA-Z0-9-]+\.[a-zA-Z]{2,64}?$/gi;
 return re.test(v);
}

Usage

isValidDomain('example.com') // true
isValidDomain('foo.example.com') // true
isValidDomain('bar.foo.example.com') // true
isValidDomain('exa-mple.co.uk') // true
isValidDomain('exa_mple.com') // false
isValidDomain('example') // false
isValidDomain('ex*mple.com') // false
isValidDomain(3434) // false

On github at miguelmota/is-valid-domain

Read on miguelmota.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.