webgeek154,
It can be hard to make a regular expression that will test for domain name, even just the TLD part. Do you need to support domains besides .com? Any international domains? The more domains you need to allow the longer the regex and generally the less specific it is (i.e. it doesn't look for .com, etc but just the right number of characters in the TLD). The regex in Javascript's match for the domains you mentioned would be ...
if (field.value.match(/\.com$
// it does match
}
To add a few more TLDs, like those listed by Ronb23 would not be hard. I do have some regex that will check domain names according to specs but even those aren't fool proof and may be overkill if you know the domains entered will only be certain types.
Let me know if you have any questions or need more information.
b0lsc0tt
Main Topics
Browse All Topics





by: Ronb23Posted on 2009-05-07 at 13:28:26ID: 24330235
you can try something like this:
function vTLD(){
var knownTLDs=".com .net .org .edu .gov .biz .info";
var myURL = document.myform.url.value;
var endofString = myURL.split('.');
var ending = endofString.length - 1;
var tld = endofString[ending];
if (knownTLDs.search(tld) < 0){
alert('Invalid URL');
return false;
} else {
return true;
}
}