ok, try this, noting improper representation of ¦ character! ^(www\.)?(([a-z0-9]+(-¦\.)?)[a-z0-9])((\.[a-z]+){1,2})$
Let me explain, ^ on the front and $ on the end, option, but will make it either return a complete match, or nothing. e.g. 'www.mydom' would still return a partial match even though invalid.
(www\.)? - Optional www
([a-z0-9]+(-¦\.)?) - a letter, or number, some times, maybe 1 - or .
[a-z0-9] - domain portion must end with a proper character, prevents www.mydomain-.com
That bit wrapped in () so you can match the whole domain portion.
The '.' also allows for subdomains, e.g. www.my.domain.com
((\.[a-z]+){1,2}) - slightly different way of finding TLD, I've gone with a . and some letters a maximum of twice, e.g. .tld or .uk.tld but not .my.tld.uk
Again wrapped in () to match. I couldn't get the length checking in, but as it matches www. mydomain and .com you could easily check this afterwards, everything else is there.
The only thing I found was that if they entered an invalid tld, i.e. .my.tld.com it will still match but as (www.)(mydomain.my).tld.com
You could get around this by changing the (-¦\.) part if the domain name bit, unfortunately without using a huge OR'd statement checking every possible valid TLD, I think this is the best you can do. It's certainly worked for me. If anyone knows a better way then please post it!
You also might want to download this:
[weitz.de...]
Or Google 'Regex Coach' if it's been snipped.