Skip to content

Instantly share code, notes, and snippets.

@nielslange
Last active January 6, 2021 12:32
Show Gist options
  • Select an option

  • Save nielslange/47a0828fd784e9e5e6d1526ba7a39096 to your computer and use it in GitHub Desktop.

Select an option

Save nielslange/47a0828fd784e9e5e6d1526ba7a39096 to your computer and use it in GitHub Desktop.
JavaScript: Validate IPv4 address
// 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