The only thing it lacks is a final check at focus out for a complete ip (since now it allows partial match to let the user type it in.It would be easy to add, just pass a %V from the -vcmd option & add extra arg to func then do primarily the same checks except force the length to be exactly 4 and don't do the trick of letting the last one blank.
Try this one instead: Readers should note that the second example doesn't attempt to do all that the first example did with respect to requiring only valid decimal values for the subportions of the ip address as far as I can figure... Actually it did check the same ranges as the first one, but it did have a flaw that it didn't check for leading 0's (which it now does) and I removed some cut/paste ghost from one line.
I got a bit caught up in the differences between addresses from RFC4291 and the recommendations in RFC5952.
The prior allows for zero folding of single 16-bit 0 fields while the latter discourages this.
PHP's filter_var function can validate if an IP address is valid and can further validate if it is an IPv4 IP, IPv6 IP, or not within the private or reserved ranges. Note that the filter_var() function requires at least PHP version 5.2.0.
It is included by default with versions This will return the $ip address passed in if it's valid or false if it is not.