+ Reply to Thread
Results 1 to 7 of 7

Thread: How do I validate a URL?

  1. #1
    Bulletproof is offline Junior Member
    Join Date
    Jul 2008
    Posts
    6

    Default How do I validate a URL?

    I'm using Zend_Filter_Input to validate some input, and one of the textboxes allows you to enter a URL in. I need to validate the URL to make sure it is in the proper Website.com format. In fact, it's supposed to point to an image, so I'm hoping I can override the validator (if one already exists) so it checks for a proper image url.

  2. #2
    xorock is offline Junior Member
    Join Date
    Mar 2008
    Posts
    17

    Default

    And what is proper image url?
    image34534534534534
    image.php?gen=23423423423
    /image/7234273642734
    image.jpg -> rewrite hack.pl

  3. #3
    Bulletproof is offline Junior Member
    Join Date
    Jul 2008
    Posts
    6

    Default

    I figured it out, thanks anyway.

  4. #4
    cantona is offline Member
    Join Date
    Jul 2008
    Location
    london
    Posts
    34

    Default how?

    Hi Bulletproof

    I'm also trying to validate a URL. How did you do it?

    I need to validate URLs like:
    Code:
    http://www.websites.com
    http://subdomain.websites.com
    http://www.websites.com/directory/
    http://www.websites.com/index.php?whatever=xyz
    I've tried using Hostname, but it fails on any URLs with a directory.

    Thanks

  5. #5
    toque_101 is offline Junior Member
    Join Date
    Feb 2009
    Posts
    1

    Default Extend Zend_Validate_Abstract

    You can validate a URI using Zend_Uri but it doesn't implement the Zend_Validate_Interface. So, you'll have to roll your own. Fortunately, it's pretty easy to do so:

    [PHP]
    class Url_Validator extends Zend_Validate_Abstract
    {
    const INVALID_URL = 'invalidUrl';

    protected $_messageTemplates = array(
    self::INVALID_URL => "'%value%' is not a valid URL.",
    );

    public function isValid($value)
    {
    $valueString = (string) $value;
    $this->_setValue($valueString);

    if (!Zend_Uri::check($value)) {
    $this->_error(self::INVALID_URL);
    return false;
    }
    return true;
    }
    }

    // When creating the form:
    $website = $form->createElement('text', 'website');
    $website->addValidator(new Url_Validator);
    [/PHP]

    Hope that helps
    -steve

  6. #6
    Rhino's Avatar
    Rhino is offline Senior Member
    Join Date
    Feb 2009
    Location
    Lost Angeles, CA
    Posts
    105

    Default

    nice! that works pretty well. thanks

  7. #7
    joedevon is offline Junior Member
    Join Date
    May 2009
    Posts
    4

    Default

    How about just passing a REGEX to the validator as an alternative:
    Zend Framework: Documentation

+ Reply to Thread

Similar Threads

  1. Validate/PostCode.php GB codes
    By NicP in forum Core Infrastructure
    Replies: 2
    Last Post: 06-29-2010, 08:21 PM
  2. Validate Empty when other field is true
    By jlavere in forum Core Infrastructure
    Replies: 7
    Last Post: 10-18-2009, 07:57 PM
  3. Custom Validate textarea?
    By hogsolo in forum General Q&A on Zend Framework
    Replies: 1
    Last Post: 09-17-2009, 10:24 PM
  4. Zend Validate
    By txrandom in forum General Q&A on Zend Framework
    Replies: 2
    Last Post: 06-04-2009, 12:48 PM
  5. validate a web address
    By cantona in forum Core Infrastructure
    Replies: 0
    Last Post: 09-04-2008, 11:28 AM

Posting Permissions

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