webgeek154
asked on
>> javscript validation for url
Hi Guys,
I have a form where I'm collection a url. An example of correct input in this field would be 'google.com' or 'anyotherdomain1234.com'
So basically I guess I just need a regex or regular javascript code to make sure that it has a '.[TLD]' at the end.
Thanks!!! : )
Johnny
I have a form where I'm collection a url. An example of correct input in this field would be 'google.com' or 'anyotherdomain1234.com'
So basically I guess I just need a regex or regular javascript code to make sure that it has a '.[TLD]' at the end.
Thanks!!! : )
Johnny
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$ /i)) {
// 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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
good catch b0lsc0tt.
ahoffmann,
Your welcome! Glad to help and that my regex skills aren't gone completely. :D
webgeek154,
If you use Ahoffmann's code then please no credit to me for the change. It was very minor and would've been caught by Ahoffmann had you posted about a problem. If you are around it would be great to get some comment and feedback though. You have plenty to work with now.
bol
Your welcome! Glad to help and that my regex skills aren't gone completely. :D
webgeek154,
If you use Ahoffmann's code then please no credit to me for the change. It was very minor and would've been caught by Ahoffmann had you posted about a problem. If you are around it would be great to get some comment and feedback though. You have plenty to work with now.
bol
webgeek154,
Thanks for closing this. It seems from your comment you acknowledge we (ahoffmann and I) both helped. You probably meant to split the points and accept both comments. That didn't happen though.
I am going to use Request Attention to have a moderator reopen this and correct how it was closed. Please post if you disagree with my efforts and explain why. Thanks for the fun question and credit. I am glad I could help a bit.
bol
Thanks for closing this. It seems from your comment you acknowledge we (ahoffmann and I) both helped. You probably meant to split the points and accept both comments. That didn't happen though.
I am going to use Request Attention to have a moderator reopen this and correct how it was closed. Please post if you disagree with my efforts and explain why. Thanks for the fun question and credit. I am glad I could help a bit.
bol
ASKER
thanks guys!!!
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;
}
}