javascript URL validation

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
LVL 10
jaggernatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pravin AsarPrincipal Systems EngineerCommented:
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
JJayJayCommented:
Hmm... Do you want to check a link or the address of the current page?
0
a122178Commented:
Do you what to validate if the URL exists and available?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jaggernatAuthor Commented:
hi pravin,

code is great, but i get this javascript error

knownDomsPat  is undefined


thanks
J
0
jaggernatAuthor Commented:
>>>>>>>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
Pravin AsarPrincipal Systems EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaggernatAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.