export function validateIpv6 (a) {
const ns = []
const nh = a.split(‘::’)
if (nh.length > 2) {
return false
} else if (nh.length === 2) {
if (nh[0].startsWith(‘:’) || nh[0].endsWith(‘:’) || nh[1].startsWith(‘:’) || nh[1].endsWith(‘:’)) {
return false
}
ns.push(…(nh[0].split(‘:’).filter(a => a)))
ns.push(…(nh[1].split(‘:’).filter(a => a)))
if (ns.length > 7) {
return false
}
} else if (nh.length === 1) {
ns.push(…(nh[0].split(‘:’).filter(a => a)))
if (ns.length !== 8) {
return false
}
}
for (const n of ns) {
const match = n.match(/1{1,4}$/i)
if (!(match !== undefined && match !== null) || (match[0] && (match[0] !== n))) {
return false
}
}
return true
}
a-f0-9 ↩︎