Connecting Tech Pros Worldwide Forums | Help | Site Map

java script for url validation

Newbie
 
Join Date: Sep 2006
Posts: 1
#1: Sep 12 '06
I simply want the validation for website url checking.
But it should not start from http:// (example http://www.yahoo.com)
it should start from ww.(example www.yahhoo.com)


Thanx in advance

Newbie
 
Join Date: Jul 2007
Posts: 2
#2: Jul 27 '07

re: java script for url validation


Quote:

Originally Posted by khushwinder

I simply want the validation for website url checking.
But it should not start from http:// (example http://www.yahoo.com)
it should start from ww.(example www.yahhoo.com)


Thanx in advance

plese set fields
Expand|Select|Wrap|Line Numbers
  1. if(document.frmRegister.WebSite.value!="")
  2.       {
  3.              if (document.frmRegister.WebSite.value.indexOf ('www', 0) == -1 || document.frmRegister.WebSite.value.length < 10)
  4.              {
  5.                   if(document.frmRegister.WebSite.value.indexOf ('.com', 0) == -1 || document.frmRegister.WebSite.value.length < 10) 
  6.                     {
  7.                        alert ("Enter a valid URL")
  8.                        document.frmRegister.WebSite.focus();
  9.                        return false;
  10.                      }
  11.              }
  12.          }
Newbie
 
Join Date: Jul 2007
Posts: 2
#3: Jul 27 '07

re: java script for url validation


Expand|Select|Wrap|Line Numbers
  1. if(document.frmRegister.WebSite.value!="")
  2.       {
  3.              if (document.frmRegister.WebSite.value.indexOf ('www', 0) == -1 || document.frmRegister.WebSite.value.length < 10)
  4.              {
  5.                   if(document.frmRegister.WebSite.value.indexOf ('.com', 0) == -1 || document.frmRegister.WebSite.value.length < 10) 
  6.                     {
  7.                        alert ("Enter a valid URL")
  8.                        document.frmRegister.WebSite.focus();
  9.                        return false;
  10.                      }
  11.              }
  12.          }
Newbie
 
Join Date: Oct 2007
Posts: 4
#4: Mar 31 '08

re: java script for url validation


Hi i m new in this site.
Plz help me out, i m stuck in website validation bcoz,
i want validation in this manner,
user can only enter the website in (http://www.test.com) or either (www.test.com)
accept this type of entry it shows alert that "plz enter the website in proper manner" ,

i used ur style but it gives a problem when we enter dot(.) in multiple times like
(http://wwwwww...........test.........com) or (wwwwww..........test..........com)
pshm's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 20
#5: Apr 1 '08

re: java script for url validation


hi,
this may help you...
Expand|Select|Wrap|Line Numbers
  1.             function isURL ( txtId ){
  2.  
  3.                   var string = document.getElementById(txtId).value;    
  4.  
  5.                   if ( string.search(/^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$/) != -1 ){
  6.                             alert('Valid URL');
  7.                         return true;
  8.                   }
  9.  
  10.  
  11.                   if ( string.search(/^[http://]+[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$/) != -1 ){
  12.                             alert('valid URL');
  13.                         return true;
  14.                   }
  15.  
  16.                   alert('Not a valid URL');
  17.                   document.getElementById(txtId).select();
  18.                   document.getElementById(txtId).focus();        
  19.                   return false;
  20.  
  21.             }
  22.  
  23.  
regards,
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Apr 1 '08

re: java script for url validation


Not quite. That would match some invalid ones and not match a lot of valid ones.

However, you've got the right idea with using regular expressions.

Which one you use depends on how accurate you wish to be. You can find some good ones by searching for "url regular expression".
Newbie
 
Join Date: Oct 2007
Posts: 4
#7: Apr 7 '08

re: java script for url validation


i had used regular expression but it did not check dot, that how many dot u have in between extensions that's y i m asking for the validation ....
i had used this validation, it check for http:// an www, but again i stuck for dots....

<!--

Expand|Select|Wrap|Line Numbers
  1. if( (website != "") || (website != null) )    
  2.     {
  3.         if (website.indexOf ('http://', 0) == -1 )
  4.         {    
  5.             if (website.indexOf ('www.', 0) == -1 || website.length < 10)
  6.                {    
  7.                    if(website.indexOf ('.com', 0) == -1 || website.length < 10)
  8.                 {
  9.                     alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
  10.                     document.filmsForm.url.focus();
  11.                     document.filmsForm.url.value = "" ;
  12.                     return false;
  13.                 }
  14.                }
  15.  
  16.             var split = website.split("\.");
  17.  
  18.             if(isNaN(split[0]))
  19.             {
  20.                 if( (split[0].length > 3) || (split[0].length < 2) )
  21.                 {
  22.                     alert("The Length Of www Must Be '3' !");
  23.                       document.filmsForm.url.focus();
  24.                     document.filmsForm.url.value = "" ;
  25.                       return false;
  26.  
  27.                 }
  28.             }else 
  29.             {
  30.                 alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
  31.                 document.filmsForm.url.focus();
  32.                 document.filmsForm.url.value = "" ;
  33.                 return false;
  34.             }
  35.  
  36.  
  37.         } else {
  38.  
  39.             if (website.indexOf('www.', 0) == -1 || website.length < 10)
  40.                {    
  41.                    if(website.indexOf('.com', 0) == -1 || website.length < 10)
  42.                 {
  43.                     alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
  44.                     document.filmsForm.url.focus();
  45.                     document.filmsForm.url.value = "" ;
  46.                     return false;
  47.                 }
  48.                }
  49.  
  50.             var splitfromhttp = website.split("http://");
  51.  
  52.             var splitfromdot = splitfromhttp[1].split("\.");
  53.  
  54.             if(isNaN(splitfromdot[0]))
  55.             {
  56.                 if( (splitfromdot[0].length > 3) || (splitfromdot[0].length < 2) )
  57.                 {
  58.                     alert("The Length Of www Must Be '3' !");
  59.                       document.filmsForm.url.focus();
  60.                     document.filmsForm.url.value = "" ;
  61.                       return false;
  62.  
  63.                 }
  64.             }else 
  65.             {
  66.                 alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
  67.                 document.filmsForm.url.focus();
  68.                 document.filmsForm.url.value = "" ;
  69.                 return false;
  70.             }
  71.  
  72.         }       
  73.     }
  74.  
-->
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Apr 7 '08

re: java script for url validation


Rather than make so many checks, just use one regular expression, e.g.:
Expand|Select|Wrap|Line Numbers
  1. /^(http://)?www\.[a-z]+\.com$/gi
A very simple one which would only match letter URLs, not ones with numbers, hyphens, etc. It would also only match .com which is what you seem to be checking for.
Newbie
 
Join Date: Oct 2007
Posts: 4
#9: Apr 14 '08

re: java script for url validation


Quote:

Originally Posted by acoder

Rather than make so many checks, just use one regular expression, e.g.:

Expand|Select|Wrap|Line Numbers
  1. /^(http://)?www\.[a-z]+\.com$/gi
A very simple one which would only match letter URLs, not ones with numbers, hyphens, etc. It would also only match .com which is what you seem to be checking for.


Hey i test this regular expression on a software (RegesxBilder) but it shows that the regular expression is worng.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#10: Apr 14 '08

re: java script for url validation


That was a quick example I came up with without testing. You'll need to escape the forward slashes:
Expand|Select|Wrap|Line Numbers
  1. /^(http:\/\/)?www\.[a-z]+\.com$/gi
Note that this is by no means a complete test. For that, you will need something far more complex.
Newbie
 
Join Date: Oct 2007
Posts: 4
#11: Apr 14 '08

re: java script for url validation


Quote:

Originally Posted by acoder

That was a quick example I came up with without testing. You'll need to escape the forward slashes:

Expand|Select|Wrap|Line Numbers
  1. /^(http:\/\/)?www\.[a-z]+\.com$/gi
Note that this is by no means a complete test. For that, you will need something far more complex.


Thanx I got my Solution through regular Expression ....Yeah! u r right its(regular expression) too easy to validate the url . And for other validation too. thanx a lot
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#12: Apr 14 '08

re: java script for url validation


You're welcome. Glad it's working!
Reply