Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: URL Validation

  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation URL Validation

    I have some javascript code for a URL validation but it doesnt seem to want to work with the following url... (i think its the " ~ " which is causeing the problem)

    code
    var w = new RegExp();
    w.compile("(ftp|http|https)+://[www]+\\.[A-Za-z0-9-_%&\?\/.=]+$");

    example url
    http://www.yourdomain.ie/~lgb/test_in_image.png

    Also, can anyone recode this so that the following url would also work..

    example url
    http://sub.yourdomain.ie/test_in_image.png

    Can anyone help?

    Thanks.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    add the ~ to the regualr expression than

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I tried that but it didnt seem to work. What about the other url using a sub domain rather than www ?

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    change [www]+ to (www)?

    Eric
    Last edited by A1ien51; 06-08-2007 at 04:17 PM.
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I am not sure how you have derived your regex but it is completely wrong.

    e.g. [www] means match w or w or w, not www.

    The “safe” characters in a URL are alphanumerics (A to Z, a to z and 0 to 9) and the following special characters: the dollar sign (“$”), hyphen (“-”), underscore (“_”), period (“.”), plus sign (“+”), exclamation point (“!”), asterisk (“*”), apostrophe (“'”), left parenthesis (“(”), and right parenthesis (“)”).

    The regex you desire (i.e. validate a URL) is:-

    ^(ftp|http|https):\/\/www[\w%&\$\-\?\/\.!\+\*\(\)\s=~]+$

    & and ? cannot normally appear as part of a URL except in query string.

    A tilde ~ is not "safe" character but I have included it. A space should be encoded as %20

    What is w.compile supposed to do?

    In fact it may be simpler and just as effective to just check the starting characters in the URL up to www.

    ^(ftp|http|https):\/\/www\.

    You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html
    Last edited by Philip M; 06-08-2007 at 04:41 PM. Reason: Added simple check.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •