Validate URL script help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MJ

    #1

    Validate URL script help

    For some reason the following script does not work in Netscape/Mozilla, but
    works fine in IE and Opera. It is supposed to check the syntax, make sure
    there is a valid TLD (yes, those are all of the current TLDs), and allow for
    addresses with or without trailing slashes or page addresses.

    Anybody have any ideas on how to get this to work in Netscape? I suspect it
    has something to do with the regular expression, but I can't get it to work.
    Any help would be GREALY appreciated!

    // Validate URL
    re3 =
    /^(http|https):\/\/\S+\.(ac|ad|ae| aero|af|ag|ai|a l|am|an|ao|aq|a r|arpa|as|at
    |au|aw|az|ba|bb |bd|be|bf|bg|bh |bi|biz|bj|bm|b n|bo|br|bs|bt|b v|bw|by|bz|ca|c c
    |cd|cf|cg|ch|ci |ck|cl|cm|cn|co |com|coop|cr|cu |cv|cx|cy|cz|de |dj|dk|dm|do|dz |
    ec|edu|ee|eg|er |es|et|fi|fj|fk |fm|fo|fr|ga|gb |gd|ge|gf|gg|gh |gi|gl|gm|gn|go v
    |gp|gq|gr|gs|gt |gu|gw|gy|hk|hm |hn|hr|ht|hu|id |ie|il|im|in|in fo|int|io|iq|ir |
    is|it|je|jm|jo| jp|ke|kg|kh|ki| km|kn|kr|kw|ky| kz|la|lb|lc|li| lk|lr|ls|lt|lu| l
    v|ly|ma|mc|md|m g|mh|mil|mk|ml| mm|mn|mo|mp|mq| mr|ms|mt|mu|mus eum|mv|mw|mx|my |
    mz|na|name|nc|n e|net|nf|ng|ni| nl|no|np|nr|nu| nz|om|org|pa|pe |pf|pg|ph|pk|pl |
    pm|pn|pr|pro|ps |pt|pw|py|qa|re |ro|ru|rw|sa|sb |sc|sd|se|sg|sh |si|sj|sk|sl|sm |
    sn|so|sr|st|su| sv|sy|sz|tc|td| tf|tg|th|tj|tk| tm|tn|to|tp|tr| tt|tv|tw|tz|ua| u
    g|uk|um|us|uy|u z|va|vc|ve|vg|v i|vn|vu|wf|ws|y e|yt|yu|za|zm|z w)([/]\S+|)$/i;

    function validateURL(tex tfield){
    if (textfield.valu e == ""){
    return true;
    } else {
    if (textfield.valu e.substring(0,7 ) != "http://" &&
    textfield.value .substring(0,8) != "https://") {
    textfield.value = "http://" + textfield.value ;
    }
    if (!re3.test(text field.value)){
    alert("Invalid web site address");
    textfield.focus ();
    }
    return false;
    }
    }

    It is being called by a simple:

    <input name="Website" type="text" onBlur="validat eURL(this)">


  • Lasse Reichstein Nielsen

    #2
    Re: Validate URL script help

    "MJ" <no_spam@thank. you> writes:
    [color=blue]
    > For some reason the following script does not work in Netscape/Mozilla, but
    > works fine in IE and Opera.[/color]

    "does not work" how? Do you get an error message or does it accept the wrong
    strings?
    [color=blue]
    > /^(http|https):\/\/\S+\.(ac|ad|ae| aero|af|ag|ai|a l|am|an|ao|aq|a r|arpa|as|at
    > |au|aw|az|ba|bb |bd|be|bf|bg|bh |bi|biz|bj|bm|b n|bo|br|bs|bt|b v|bw|by|bz|ca|c c[/color]

    Your newsclient has wrapped the line. It should be on one line to work.
    [color=blue]
    > g|uk|um|us|uy|u z|va|vc|ve|vg|v i|vn|vu|wf|ws|y e|yt|yu|za|zm|z w)([/]\S+|)$/i;[/color]
    ^
    That slash should be escaped. Change "[/]" to "\/".

    Not tested (didn't want to rewrap the lines :)
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • MJ

      #3
      Re: Validate URL script help

      Ah, it was the regular expression. Escaping the slash fixed it. I could
      have sworn I had tried that before, but I guess not.

      Thanks for the help! You're a life saver.


      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:llj9tnpg.f sf@hotpop.com.. .[color=blue]
      > "MJ" <no_spam@thank. you> writes:
      >[color=green]
      > > For some reason the following script does not work in Netscape/Mozilla,[/color][/color]
      but[color=blue][color=green]
      > > works fine in IE and Opera.[/color]
      >
      > "does not work" how? Do you get an error message or does it accept the[/color]
      wrong[color=blue]
      > strings?
      >[color=green]
      > >[/color][/color]
      /^(http|https):\/\/\S+\.(ac|ad|ae| aero|af|ag|ai|a l|am|an|ao|aq|a r|arpa|as|at[color=blue][color=green]
      > >[/color][/color]
      |au|aw|az|ba|bb |bd|be|bf|bg|bh |bi|biz|bj|bm|b n|bo|br|bs|bt|b v|bw|by|bz|ca|c c[color=blue]
      >
      > Your newsclient has wrapped the line. It should be on one line to work.
      >[color=green]
      > >[/color][/color]
      g|uk|um|us|uy|u z|va|vc|ve|vg|v i|vn|vu|wf|ws|y e|yt|yu|za|zm|z w)([/]\S+|)$/i;[color=blue]
      > ^
      > That slash should be escaped. Change "[/]" to "\/".
      >
      > Not tested (didn't want to rewrap the lines :)
      > /L
      > --
      > Lasse Reichstein Nielsen - lrn@hotpop.com
      > DHTML Death Colors:[/color]
      <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
      > 'Faith without judgement merely degrades the spirit divine.'[/color]


      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Validate URL script help

        MJ wrote:
        [color=blue]
        > // Validate URL re3 = /^(http|https):[/color]

        The alternation can be written as /https?/ which is generally
        more efficient ("http" can be matched always only once).
        [color=blue]
        > \/\/\S+\.(ac|ad|ae| aero|af|ag|ai|a l|am|an|ao|aq|a r|arpa|as|at
        > |au|aw|az|ba|bb |bd|be|bf|bg|bh |bi|biz|bj|bm|b n|bo|br|bs|bt|b v|bw|by|bz|ca|c c
        > |cd|cf|cg|ch|ci |ck|cl|cm|cn|co |com|coop|cr|cu |cv|cx|cy|cz|de |dj|dk|dm|do|dz |
        > ec|edu|ee|eg|er |es|et|fi|fj|fk |fm|fo|fr|ga|gb |gd|ge|gf|gg|gh |gi|gl|gm|gn|go v
        > |gp|gq|gr|gs|gt |gu|gw|gy|hk|hm |hn|hr|ht|hu|id |ie|il|im|in|in fo|int|io|iq|ir |
        > is|it|je|jm|jo| jp|ke|kg|kh|ki| km|kn|kr|kw|ky| kz|la|lb|lc|li| lk|lr|ls|lt|lu| l
        > v|ly|ma|mc|md|m g|mh|mil|mk|ml| mm|mn|mo|mp|mq| mr|ms|mt|mu|mus eum|mv|mw|mx|my |
        > mz|na|name|nc|n e|net|nf|ng|ni| nl|no|np|nr|nu| nz|om|org|pa|pe |pf|pg|ph|pk|pl |
        > pm|pn|pr|pro|ps |pt|pw|py|qa|re |ro|ru|rw|sa|sb |sc|sd|se|sg|sh |si|sj|sk|sl|sm |
        > sn|so|sr|st|su| sv|sy|sz|tc|td| tf|tg|th|tj|tk| tm|tn|to|tp|tr| tt|tv|tw|tz|ua| u
        > g|uk|um|us|uy|u z|va|vc|ve|vg|v i|vn|vu|wf|ws|y e|yt|yu|za|zm|z w)[/color]

        That can be shortened very much to

        \/\/\S+\.(aero|arpa |a[c-gilm-oq-uwz]|biz|b[a-bd-fjm-or-tvwyz]|com|coop
        |c[acdf-ik-oruvx-z|d[ejkmoz]|edu|e[cegr-t]|f[i-kmor]|gov|g[abd-il-npr-uwy]
        |h[kmnrtu]|info|int|i[del-oqr-t]|j[emop]|k[eghimnrwyz]|l[abcikr-vy]
        |museum|mil|m[acdghkl-z]|name|net|n[acefgilopruz]|org|om|pro|p[aefghk-nr-twy]
        |qa|r[eouw]|s[a-eg-ort-vyz]|t[cdf-hjkm-prtvwz]|u[agkmsyz]|v[aceginu]
        |w[fs]|y[etu]|z[amw])

        (if I have not missed a character or two, but I think you get the
        idea). That is not only shorter but can be more efficient than
        complete alternation, depending on the type of RegExp engine used.
        With a NFA, character classes are much more efficient than alternation
        because matching can be done in parallel and thus much faster. (See
        <http://www.oreilly.com/catalog/regex/chapter/ch04.html>, "Character
        Classes vs. Alternation".) For ECMAScript and implementations , a NFA
        is clearly involved in the matching process, if not the only engine
        type used, as backreferences and capturing parantheses are supported.
        So it is clearly a Good Thing to replace alternation with character
        classes here and avoid alternation where possible.

        But:

        Are you sure you need the top level domain this precise while
        the rest is checked rather sloppy? Are you prepared to maintain
        that script as top level domains evolve? Why don't you allow
        IPv4 addresses in URLs? They can be static. Why don't you stick
        (close) to RFC 2396? The BNF grammar can be easily implemented
        as a RegExp.
        [color=blue]
        > ([/]\S+|)$/i;[/color]

        As Lasse already pointed out, in RegExp literals every forward slash
        must be escaped, even in character classes. In fact, the character
        class does not help here. But you probably meant /((\/\S+)?)$/i.
        Rule of thumb: Do not use alternation when not necessary, see above.


        PointedEars

        P.S.
        Please take heed of Usenet/Internet standards and use an existing "From:"
        address. Avoiding spam is no excuse for breaking standards and thus helping
        to destroy the functionality of the involved media:
        <http://www.interhack.n et/pubs/munging-harmful/>

        Comment

        Working...