Last active
January 6, 2021 12:32
-
-
Save nielslange/47a0828fd784e9e5e6d1526ba7a39096 to your computer and use it in GitHub Desktop.
JavaScript: Validate IPv4 address
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
| // Validate IPv4 address | |
| function isValidIPv4(string) { | |
| const octet = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)'; | |
| const regex = new RegExp(`^${octet}\\.${octet}\\.${octet}\\.${octet}$`); | |
| return regex.test(string) ? true : false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment