Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ipv6: function (val, req, attribute) {
- if (typeof val != 'string')
- return false;
- // regex to check that each hextet is valid
- var er = /^[0-9a-f]+$/;
- // ipv6 hextets are delimited by colon
- hextets = val.split(':');
- // check 1: ipv6 should contain only one consecutive colons
- colons = val.match(/::/);
- if (colons != null && val.match(/::/g).length > 1)
- return false;
- // check 2: ipv6 should not be ending or starting with colon
- // edge case: not with consecutive colons
- if (val[0] == ':' && (colons == null || (colons != null && colons.index != 0)))
- return false;
- if (val[val.length - 1] == ':' && (colons == null || (colons != null && colons.index != val.length - 2)))
- return false;
- // check 3: ipv6 should contain no less than 3 sector
- // minimum ipv6 addres - ::1
- if (3 > hextets.length)
- return false;
- // check 4: ipv6 should contain no more than 8 sectors
- // only 1 edge case: when first or last sector is ommited
- var isEdgeCase = (hextets.length == 9 && colons != null && (colons.index == 0 || colons.index == val.length - 2));
- if (hextets.length > 8 && !isEdgeCase)
- return false;
- // check 5: ipv6 should contain exactly one consecutive colons if it has less than 8 sectors
- if (hextets.length != 8 && colons == null)
- return false;
- for (let i = 0; i < hextets.length; i++) {
- const element = hextets[i];
- if (element.length == 0)
- continue;
- // check 6: all of hextets should contain numbers from 0 to f (in hexadecimal)
- if (!er.test(element))
- return false;
- // check 7: all of hextet values should be less then ffff (in hexadeimal)
- // checking using length of hextet. lowest invalid value's length is 5.
- // so all valid hextets are length of 4 or less
- if (element.length > 4)
- return false;
- }
- return true;
- }
Add Comment
Please, Sign In to add comment