>>  javscript validation for url

Posted on 2009-05-07
Last Modified: 2012-05-06
Hi Guys,

I have a form where I'm collection a url.  An example of correct input in this field would be '' or ''

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!!!  : )


Question by:webgeek154

Expert Comment

ID: 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 ( < 0){
      alert('Invalid URL');
      return false;
   } else {
      return true;
LVL 54

Expert Comment

ID: 24338149

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.

LVL 51

Accepted Solution

ahoffmann earned 250 total points
ID: 24354494
if (field.value.match(/\.(?biz|com|de|es|fr|ga|hu|info|jp|za)$/i)) {
   // it does match; but ther're much more TLDs
LVL 54

Assisted Solution

b0lsc0tt earned 250 total points
ID: 24356521
It is a Monday and I have been using Regexes a little less recently so maybe I am wrong but I think you have a small typo.  Doesn't the ? character in your regex need the colon after it.  I believe you are trying to make that group a non-capturing group.  Just wanted to post this in case I am right so there wouldn't be a problem with the Asker using your code.
if (field.value.match(/\.(?:biz|com|de|es|fr|ga|hu|info|jp|za)$/i)) {
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

LVL 51

Expert Comment

ID: 24359156
good catch b0lsc0tt.
LVL 54

Expert Comment

ID: 24359340
Your welcome!  Glad to help and that my regex skills aren't gone completely. :D
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.
LVL 54

Expert Comment

ID: 24773653
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.

Author Closing Comment

ID: 31579172
thanks guys!!!

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Close "X" button on window popup in Firefox 7 34
jQuery detect if it is a mobile device 3 91
jQuery Parallax 1 38
Materializecss - Different Behaviors in Browser's 1 17
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now