the domain name contains 4 www's
That is allowed.
and the last domain name doesnt exit
so it is invalid.
That the domain name is not registered or that the website at that domain name is not accessible does not mean that the domain name is invalid.
how do i ckeck for an invalid domain name?
One way is to write a regular expression to match the rules concerning valid domain names. However, what you are interested in is not just checking for validity, but checking that the website at that domain name can be accessed. To do this, there is no way out: you have to attempt to connect to the website, which is precisely what you were trying to avoid.