Skip to content

Instantly share code, notes, and snippets.

@i07
Last active August 29, 2015 14:13
Show Gist options
  • Save i07/2df72a66cf56dd3ad146 to your computer and use it in GitHub Desktop.
Save i07/2df72a66cf56dd3ad146 to your computer and use it in GitHub Desktop.
Validate an IP
function validate_ip(ipaddr)
{
if (/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/.test(ipaddr))
return true;
return false;
}
usage:
<script>
if ( validate_ip("192.168.10.10") ) {
/* ip is valid */
} else {
/* ip is not valid */
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment