Search code examples
ipv6prefixsubnet

How to validate IPV6 network prefix in javascript


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::

Solution

  • 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");
    }