?
Solved

javascript URL validation

Posted on 2006-04-25
7
Medium Priority
?
527 Views
Last Modified: 2008-01-09
Dear experts

I would like to know if there are any resources available for URL validation

i want to validate urls like www.yahoo.com. any source code is greatly appreciated

thanks
J
0
Comment
Question by:jaggernat
7 Comments
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16535204
Hope this helps

function isURL(urlStr){
      if (urlStr.indexOf(" ")!=-1){
            alert("Spaces are not allowed in a URL");
            return false;
      }
      if(urlStr==""||urlStr==null){
            return false;
      }

      urlStr=urlStr.toLowerCase();
      var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
      var validChars="\[^\\s" + specialChars + "\]";
      var atom=validChars + '+';
      var urlPat=/^http:\/\/(\w*)\.([\-\+a-z0-9]*)\.(\w*)/;
      var matchArray=urlStr.match(urlPat);
      if (matchArray==null){
            alert("The URL seems incorrect \ncheck it begins with http://\n and it has 2 .'s");
            return false;
      }
      var user=matchArray[2];
      var domain=matchArray[3];
      for (i=0; i<user.length; i++) {
            if (user.charCodeAt(i)>127) {
                  alert("This domain contains invalid characters.");
                  return false;
            }
      }
      for (i=0; i<domain.length; i++) {
            if (domain.charCodeAt(i)>127) {
                  alert("This domain name contains invalid characters.");
                  return false;
            }
      }
      var atomPat=new RegExp("^" + atom + "$");
      var domArr=domain.split(".");
      var len=domArr.length;
      for (i=0;i<len;i++) {
            if (domArr[i].search(atomPat)==-1) {
                  alert("The domain name does not seem to be valid.");
                  return false;
            }
      }
      if (domArr[domArr.length-1].length!=2 && 
          domArr[domArr.length-1].search(knownDomsPat)==-1) {
            alert("The address must end in a well-known domain or two letter " + "country.");
            return false;
      }
        // All Done.
      return true;
}
0
 
LVL 1

Expert Comment

by:JJayJay
ID: 16535324
Hmm... Do you want to check a link or the address of the current page?
0
 
LVL 3

Expert Comment

by:a122178
ID: 16535726
Do you what to validate if the URL exists and available?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:jaggernat
ID: 16535784
hi pravin,

code is great, but i get this javascript error

knownDomsPat  is undefined


thanks
J
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16535798
>>>>>>>Do you what to validate if the URL exists and available?

NO

Just for fronend validation. want to check if user has entered something like
http://www.yahoo.com or even www.yahoo.com is fine


thanks
0
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 1600 total points
ID: 16539370
I guess , in hurry  I missed to post teh knownDomsPat;
This string with domain extensions are okay...


function isURL(urlStr){
     if (urlStr.indexOf(" ")!=-1){
          alert("Spaces are not allowed in a URL");
          return false;
     }
     if(urlStr==""||urlStr==null){
          return false;
     }

     urlStr=urlStr.toLowerCase();
     var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
     var validChars="\[^\\s" + specialChars + "\]";
     var atom=validChars + '+';
     var urlPat=/^http:\/\/(\w*)\.([\-\+a-z0-9]*)\.(\w*)/;
     var matchArray=urlStr.match(urlPat);
     if (matchArray==null){
          alert("The URL seems incorrect \ncheck it begins with http://\n and it has 2 .'s");
          return false;
     }
     var user=matchArray[2];
     var domain=matchArray[3];
     for (i=0; i<user.length; i++) {
          if (user.charCodeAt(i)>127) {
               alert("This domain contains invalid characters.");
               return false;
          }
     }
     for (i=0; i<domain.length; i++) {
          if (domain.charCodeAt(i)>127) {
               alert("This domain name contains invalid characters.");
               return false;
          }
     }
     var atomPat=new RegExp("^" + atom + "$");
     var domArr=domain.split(".");
     var len=domArr.length;
     for (i=0;i<len;i++) {
          if (domArr[i].search(atomPat)==-1) {
               alert("The domain name does not seem to be valid.");
               return false;
          }
     }

     //
    // Permit these domain extensions...
    //
    var knownDomsPat = /[com|tv|co.in]/;

     if (domArr[domArr.length-1].length!=2 && 
         domArr[domArr.length-1].search(knownDomsPat)==-1) {
          alert("The address must end in a well-known domain or two letter " + "country.");
          return false;
     }
        // All Done.
       alert ('URL ' + urlStr + ' Okay');
     return true;
}
isURL("http://www.yahoo.com");
isURL("http://www.yahoo.xxx");
isURL("http://www.yahoo.co.in");
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16548176
thanks
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

621 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