java script for url validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khushwinder
    New Member
    • Sep 2006
    • 1

    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
  • rajeshbhatiya
    New Member
    • Jul 2007
    • 2

    #2
    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
    [CODE=javascript]if(document.frm Register.WebSit e.value!="")
    {
    if (document.frmRe gister.WebSite. value.indexOf ('www', 0) == -1 || document.frmReg ister.WebSite.v alue.length < 10)
    {
    if(document.frm Register.WebSit e.value.indexOf ('.com', 0) == -1 || document.frmReg ister.WebSite.v alue.length < 10)
    {
    alert ("Enter a valid URL")
    document.frmReg ister.WebSite.f ocus();
    return false;
    }
    }
    }[/CODE]
    Last edited by gits; Jul 27 '07, 10:56 AM. Reason: added CODE tags

    Comment

    • rajeshbhatiya
      New Member
      • Jul 2007
      • 2

      #3
      [CODE=javascript]if(document.frm Register.WebSit e.value!="")
      {
      if (document.frmRe gister.WebSite. value.indexOf ('www', 0) == -1 || document.frmReg ister.WebSite.v alue.length < 10)
      {
      if(document.frm Register.WebSit e.value.indexOf ('.com', 0) == -1 || document.frmReg ister.WebSite.v alue.length < 10)
      {
      alert ("Enter a valid URL")
      document.frmReg ister.WebSite.f ocus();
      return false;
      }
      }
      }[/CODE]

      Comment

      • christabhi
        New Member
        • Oct 2007
        • 4

        #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)

        Comment

        • pshm
          New Member
          • Mar 2008
          • 20

          #5
          hi,
          this may help you...
          [code=javascript]
          function isURL ( txtId ){

          var string = document.getEle mentById(txtId) .value;

          if ( string.search(/^[a-zA-Z0-9\-\.]+\.(com|org|net |mil|edu|COM|OR G|NET|MIL|EDU)$/) != -1 ){
          alert('Valid URL');
          return true;
          }


          if ( string.search(/^[http://]+[a-zA-Z0-9\-\.]+\.(com|org|net |mil|edu|COM|OR G|NET|MIL|EDU)$/) != -1 ){
          alert('valid URL');
          return true;
          }

          alert('Not a valid URL');
          document.getEle mentById(txtId) .select();
          document.getEle mentById(txtId) .focus();
          return false;

          }

          [/code]
          regards,

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            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".

            Comment

            • christabhi
              New Member
              • Oct 2007
              • 4

              #7
              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....

              <!--

              [CODE=javascript]if( (website != "") || (website != null) )
              {
              if (website.indexO f ('http://', 0) == -1 )
              {
              if (website.indexO f ('www.', 0) == -1 || website.length < 10)
              {
              if(website.inde xOf ('.com', 0) == -1 || website.length < 10)
              {
              alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
              document.filmsF orm.url.focus() ;
              document.filmsF orm.url.value = "" ;
              return false;
              }
              }

              var split = website.split(" \.");

              if(isNaN(split[0]))
              {
              if( (split[0].length > 3) || (split[0].length < 2) )
              {
              alert("The Length Of www Must Be '3' !");
              document.filmsF orm.url.focus() ;
              document.filmsF orm.url.value = "" ;
              return false;

              }
              }else
              {
              alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
              document.filmsF orm.url.focus() ;
              document.filmsF orm.url.value = "" ;
              return false;
              }


              } else {

              if (website.indexO f('www.', 0) == -1 || website.length < 10)
              {
              if(website.inde xOf('.com', 0) == -1 || website.length < 10)
              {
              alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
              document.filmsF orm.url.focus() ;
              document.filmsF orm.url.value = "" ;
              return false;
              }
              }

              var splitfromhttp = website.split(" http://");

              var splitfromdot = splitfromhttp[1].split("\.");

              if(isNaN(splitf romdot[0]))
              {
              if( (splitfromdot[0].length > 3) || (splitfromdot[0].length < 2) )
              {
              alert("The Length Of www Must Be '3' !");
              document.filmsF orm.url.focus() ;
              document.filmsF orm.url.value = "" ;
              return false;

              }
              }else
              {
              alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
              document.filmsF orm.url.focus() ;
              document.filmsF orm.url.value = "" ;
              return false;
              }

              }
              }
              [/CODE]
              -->
              Last edited by acoder; Apr 7 '08, 09:03 AM. Reason: Added code tags

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Rather than make so many checks, just use one regular expression, e.g.:
                [code=javascript]/^(http://)?www\.[a-z]+\.com$/gi[/code]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.

                Comment

                • christabhi
                  New Member
                  • Oct 2007
                  • 4

                  #9
                  Originally posted by acoder
                  Rather than make so many checks, just use one regular expression, e.g.:
                  [code=javascript]/^(http://)?www\.[a-z]+\.com$/gi[/code]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.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    That was a quick example I came up with without testing. You'll need to escape the forward slashes:[code=javascript]/^(http:\/\/)?www\.[a-z]+\.com$/gi[/code]Note that this is by no means a complete test. For that, you will need something far more complex.

                    Comment

                    • christabhi
                      New Member
                      • Oct 2007
                      • 4

                      #11
                      Originally posted by acoder
                      That was a quick example I came up with without testing. You'll need to escape the forward slashes:[code=javascript]/^(http:\/\/)?www\.[a-z]+\.com$/gi[/code]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

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        You're welcome. Glad it's working!

                        Comment

                        Working...