Last active
January 21, 2019 18:38
-
-
Save dmurawsky/7af77e756988bd35882d1b9591cc9a4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValidDomain(v, opts) { | |
if (typeof v !== 'string') return false | |
if (!(opts instanceof Object)) opts = {} | |
var parts = v.split('.') | |
if (parts.length <= 1) return false | |
var tld = parts.pop() | |
var tldRegex = /^(?:xn--)?[a-zA-Z0-9]+$/gi | |
if (!tldRegex.test(tld)) return false | |
if (opts.subdomain == false && parts.length > 1) return false | |
var isValid = parts.every(function(host, index) { | |
if (opts.wildcard && index === 0 && host === '*' && parts.length > 1) return true | |
var hostRegex = /^(?!:\/\/)([a-zA-Z0-9]+|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])$/gi; | |
return hostRegex.test(host) | |
}) | |
return isValid | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var domain = $('#domainInput').val(); | |
if(isValidDomain(domain)) { | |
// analyze | |
} else { | |
$('#error').text('Invalid domain'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment