Last active
May 18, 2022 03:46
-
-
Save dbwodlf3/7dfe006d71d41573aa7f9dcaa4148f61 to your computer and use it in GitHub Desktop.
URL Validation
This file contains hidden or 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 isUrl(inputString){ | |
| // Pattern 1 protocol://domain.tld/paths or protocol://subdomain.domain.tld/paths | |
| // Pattern 2 domain.tld/paths | |
| const allowed_protocols = ["http", "https"]; | |
| let input_string = inputString; | |
| let protocol = ""; | |
| let hostname = ""; | |
| let subdomain = ""; | |
| let domain = ""; | |
| let tld = ""; | |
| let paths = ""; | |
| let temp; | |
| for(const _protocol of allowed_protocols) { | |
| let isProtocol = _protocol == input_string.substring(0, _protocol.length); if(!isProtocol) continue; | |
| let splitter = "://" == input_string.substring(_protocol.length, _protocol.length+3); if(!splitter) break; | |
| protocol = _protocol; | |
| } | |
| if(protocol) input_string = input_string.substring(protocol.length+3); | |
| temp = input_string.split("/"); | |
| hostname = temp.shift(); | |
| paths = temp.join("/"); | |
| temp = hostname.split("."); | |
| if(temp.length == 3) return true; | |
| else if(temp.length == 2) return true; | |
| else return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment