-2

I am trying to validate certain IPV6 network prefix to create a subnet. I am unable to validate the network prefix using JavaScript regular expression. Can anyone help.

Format of some network prefix as below

2001:0000:1234:0000 
2001:0:1234:0
3ffe:0b00::
3ffe:b00::1
FF02::
FF02::1
::1
::
::ffff:192
3ffe:0b00:0000:0001
FF02:0000:0000:0000
FF02::
ff02::1 
fe80::
MANOJ
  • 177
  • 5
  • 14

2 Answers2

0

Now i could able to validate the Network prefix using the below set of code.

var valueToTest=document.getElementById('networkprefix').value;

if ( /^((?:[0-9A-Fa-f]{1,4}))*:((?:[0-9A-Fa-f]{1,4}))*:((?:[0-9A-Fa-f]{1,4}))*:((?:[0-9A-Fa-f]{1,4}))$/g.test(valueToTest)) 
{
    alert("Valid Network Prefix");

}
else
{
    alert("InValid Network Prefix");
}
MANOJ
  • 177
  • 5
  • 14
0

The answer you provided is inadequate to properly validate all valid IPv6 address formats for text representation.

If you want to capture any of the valid RFC 4291, IP Version 6 Addressing Architecture formats, then you need a regular expression like:

^(?:(?:(?:[A-F0-9]{1,4}:){5}[A-F0-9]{1,4}|(?:[A-F0-9]{1,4}:){4}:[A-F0-9]{1,4}|(?:[A-F0-9]{1,4}:){3}(?::[A-F0-9]{1,4}){1,2}|(?:[A-F0-9]{1,4}:){2}(?::[A-F0-9]{1,4}){1,3}|[A-F0-9]{1,4}:(?::[A-F0-9]{1,4}){1,4}|(?:[A-F0-9]{1,4}:){1,5}|:(?::[A-F0-9]{1,4}){1,5}|:):(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])|(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}|(?:[A-F0-9]{1,4}:){6}:[A-F0-9]{1,4}|(?:[A-F0-9]{1,4}:){5}(?::[A-F0-9]{1,4}){1,2}|(?:[A-F0-9]{1,4}:){4}(?::[A-F0-9]{1,4}){1,3}|(?:[A-F0-9]{1,4}:){3}(?::[A-F0-9]{1,4}){1,4}|(?:[A-F0-9]{1,4}:){2}(?::[A-F0-9]{1,4}){1,5}|[A-F0-9]{1,4}:(?::[A-F0-9]{1,4}){1,6}|(?:[A-F0-9]{1,4}:){1,7}:|:(?::[A-F0-9]{1,4}){1,7}|::)$
Community
  • 1
  • 1
Ron Maupin
  • 6,180
  • 4
  • 29
  • 36