www.webdeveloper.com
Results 1 to 5 of 5

Thread: URL validation function in question...

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    URL validation function in question...

    here's another validation function i was trying out.
    i don't remember where i got this.

    PHP Code:
    function validateURL($URL) {
        
    $v "/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";
        return (bool)
    preg_match($v$URL);

    i thought it was working until i tried this..

    PHP Code:
    $url4 "http://www.websiteaddr\.{}ess";

    if(
    validateURL($url4)) {
        echo 
    "<p>URL 4 is valid</p>";
    } else {
        echo 
    "<p>URL 4 is not valid</p>";

    am i using this correctly? it's suppose to return a boolean right?

    it echos that url4 is valid. .. and it has no dotcom or dotnet etc.

    can anyone recommend the best, most bullet proof form validation out there?

    big thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Use it in a cURL request and see if you get a valid HTTP response code.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2011
    Posts
    1,145
    I have limited skills with regular expressions, but the one you're using looks like it might have been designed for a specific situation. So it might not be suitable as a general URL validator. If you search for a new regular expression to use, because ICANN is approving so many new Top Level Domain (TLD) names, I'd suggest that you look for one that does NOT check for specific TLDs like .com or .net or TLDs of a specific length - '{2,3}'. If you're not sure, do some testing with a .info or a .name domain and see what happens.
    Rick Trethewey
    Rainbo Design

  4. #4
    Join Date
    Mar 2010
    Posts
    281
    thanks guys, though not sure what you meant Nog dog

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Quote Originally Posted by toptomato View Post
    thanks guys, though not sure what you meant Nog dog
    It would be similar to a user registration page "testing" your email address by sending you a confirmation email to that address with some link you need to click in order to complete the process.

    In this case, you could use cURL (or some other technique to send a HTTP request) using the supplied URL, and if it results in a successful response, then by definition it is a valid URL, both in terms of syntax and actually pointing to a valid web resource.

    If you are, for some reason, only concerned with syntax, then you would need to be very meticulous with any regular expression syntax in order to avoid false negatives against valid URLs which happen to break imagined "rules". This means going through the relevant specifications and ensuring you understand exactly what is and is not legal for the structure of a URL.

    (The same holds true for many email address syntax regexp's you may find out there, most of which have the potential to provide false negatives.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles