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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    London
    Posts
    179
    Thanks
    0
    Thanked 0 Times in 0 Posts

    URL validation with regular expression

    Hi all - am looking for an answer to a particular problem...

    I have a form on my site that allows visitors to provide the URL of the site... Am looking for a reg exp for validating that the URL is correctly formed...

    Anyone able to help.

    Thanks

    Dominic
    dominicall - confusing himself more and more each day

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you provide the rules for a correctly formed URL?

    What URLs are you going ot accept?

    www.codingforums.com
    http://www.codingforums.com/
    codingforums.com
    http://www.codingforums.com/newreply.php?s=&action=newreply&threadid=9241

    All are valid URLs. Which do you plan on accepting?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    London
    Posts
    179
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops - sorry beetle.... should have been clearer.

    Am only looking to validate anything http, i.e.

    http://www.codingforums.com
    http://someotherprefix.codingforums.com

    or with a querystring, i.e.

    http://www.codingforums.com?anything_here

    Am not so worried about ppl putting the http:// in front of their entered address as I can check and add if necessary... have been struggling to design the correct pattern match for the rest...

    I am getting better with reg exps but this one has me stumped... what I'm looking to do is...
    Code:
    function isValidURL(url)  {
    	if (!/regularexpressionpatternforurlhere/.test(url))  {
    		return false;
    		}
    	return true;
    	}
    Hope that makes it clearer.

    Dominic
    dominicall - confusing himself more and more each day

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, here's a first draft...

    /^(?:http:\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/i

    Breakdown:

    ^
    match at beginning of string

    (?:http:\/\/)?
    optional match of the http://

    (?:[\w-]+\.)+
    At least one group of accepted characters and a period

    [a-z]{2,6}$
    match domain at end of string.
    Last edited by beetle; 11-05-2002 at 04:05 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    London
    Posts
    179
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool - thanks beetle - works perfectly....

    Funnily enough, after I posted my message I found this...

    (((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i

    which also validates with a querystring, so have cut it down to...

    (http:\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i

    to validate http:// only

    It doesn't check for max number of characters for the TLD - so I'm going to try to decipher it tonight - which should confuse me some - LOL. Am not sure if I'll work it out though

    Thanks

    Dominic
    dominicall - confusing himself more and more each day

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How useful is checking for a maximum TLD characters length? Some months ago, ICANN added .museum to the available TLDs, and the effect: Most older regexp validating URLs needed an update, if the problem was communicated and easy to resolve (since the intent behind a regexp is difficult to see from the syntax).

    Because we don't know what TLDs might pop up in the nearer future, I would refrain from trying to set a maximum length at the moment.

    Just my 2 cents.

  • #7
    Regular Coder
    Join Date
    Sep 2002
    Location
    London
    Posts
    179
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good point mordred... who knows what ICANN are going to do next...

    Dominic
    dominicall - confusing himself more and more each day

  • #8
    Regular Coder
    Join Date
    Sep 2002
    Location
    London
    Posts
    179
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Huh.... what's going on...

    Am incorporating the URL check into my site at home but first need to check whether the submitted url starts with http:// ... so Iwrote this just to do a check see if I was getting it right...
    Code:
    function isValidURL(url)  {
    	if (!/^((http)|(HTTP)):\/\//.test(url));  {
    		alert ("oops - not OK");
    		checkurl.url.focus();
    		checkurl.url.select();
    		return false;
    		}
    	alert ("everything OK");
    	return true;
    	}
    So why does it return false even when I start the url with http:// or HTTP://

    Very confused

    Dominic
    dominicall - confusing himself more and more each day

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Because one semicolon is in the wrong place, your intended else block is always carried out and the function returns.

    Code:
    if (!/^((http)|(HTTP)):\/\//.test(url));  {
    Nothing wrong with the RegExp as far as I can see, though.

  • #10
    Regular Coder
    Join Date
    Sep 2002
    Location
    London
    Posts
    179
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmmm..... thanks mordred....

    I took out the semi-colon (the one you showed in red) and it still returns false even if I use a URL starting with http://
    Code:
    function isValidURL(url)  {
    	if (!/^((http)|(HTTP)):\/\//.test(url))  {
    		alert ("oops - not OK");
    		checkurl.url.focus();
    		checkurl.url.select();
    		return false;
    		}
    	alert ("everything OK");
    	return true;
    	}
    Any ideas????

    Dominic
    dominicall - confusing himself more and more each day

  • #11
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just do this

    if (!/^http:\/\//i.test(url)) {

    See that red i? It's called a pattern modifier, and the i modifier means case-insensitive matching, or ignore case.

    the other common modifier is g which signifies global matching, replacing.

    If you look back, you can see that I used the i modifier in my first post
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #12
    Regular Coder
    Join Date
    Sep 2002
    Location
    London
    Posts
    179
    Thanks
    0
    Thanked 0 Times in 0 Posts
    LOL - that's what I did in the first place but changed it to be verbose.... still returning the error though

    Have attached as a text file - VERY strange....

    Dominic
    Attached Files Attached Files
    dominicall - confusing himself more and more each day

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Did you check out the regular expression I'm using to validate URLs?

    http://www.solidscripts.com/displayscript.asp?sid=10

    I got it from http://www.regexlib.com/ .

    Seems to work pretty good... I haven't run into any problems with it yet, at any rate.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #14
    Owl
    Owl is offline
    New Coder
    Join Date
    Jun 2002
    Posts
    62
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi dominicall,

    <form name="checkurl" onsubmit="return isValidURL(this.url.value)">

    ( •) (• )
    >>V

  • #15
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No offense Owl, but where's the javascript function you're calling (isValidUrl())?

    Also, when you're using server-side scripting, it's a lot more reliable than javascript, since you don't have to rely on the client having javascript enabled on their browser.

    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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