By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,481 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,481 IT Pros & Developers. It's quick & easy.

java script for url validation

P: 1
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
Sep 12 '06 #1
Share this Question
Share on Google+
11 Replies


P: 2
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.          }
Jul 27 '07 #2

P: 2
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.          }
Jul 27 '07 #3

P: 4
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)
Mar 31 '08 #4

pshm
P: 20
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,
Apr 1 '08 #5

acoder
Expert Mod 15k+
P: 16,027
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".
Apr 1 '08 #6

P: 4
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.  
-->
Apr 7 '08 #7

acoder
Expert Mod 15k+
P: 16,027
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.
Apr 7 '08 #8

P: 4
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.
Apr 14 '08 #9

acoder
Expert Mod 15k+
P: 16,027
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.
Apr 14 '08 #10

P: 4
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
Apr 14 '08 #11

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Glad it's working!
Apr 14 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.