IPv4 Address (By: cheeze)
Create a function validate() that accepts a string whether it is a valid IPv4 address. Examples of IPv4 address are below. The range is assumed to be from 0.0.0.0 to 255.255.255.255.
Two more test cases added:
- Should not accept negative values
- Should not accept anything other than number e.g 1.2.3.abc //Invalid or a220.127.116.11 //Invalid
Here is my version:
Tashi's code declares strings like 127.001.001.009 as valid, but do we want these? I considered using Regex as well, but mine became ugly af, when I excluded these.
A tip for everyone who wants to learn Regex properly: Get yourself some lecture notes from a course on theoretical computer science and read about formal languages/grammars, DFAs and regular languages.
Exercise: Build something like this:
If you need an explanation, I'm happy to help.