469,293 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

java script for url validation

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
11 14410
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
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
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
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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.

Similar topics

reply views Thread by Brian | last post: by
7 posts views Thread by drum118 | last post: by
1 post views Thread by jaimemartin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.