Welcome Guest, Not a member yet? Register   Sign In
Valid url check in form validation
#1

[eluser]Iverson[/eluser]
Here's a little something you can add into the Form_validation.php to check urls. One just checks the syntax. The other actually checks to see if the website exists. Feel free to improve the regex as I'm by no means claiming to be an expert! Smile

Code:
// --------------------------------------------------------------------
    
    /**
     * Validate URL
     *
     * @access    public
     * @param    string
     * @return    string
     */
    function valid_url($url)
    {
        $pattern = "/^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?/";
        if (!preg_match($pattern, $url))
        {
            return FALSE;
        }

        return TRUE;
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Real URL
     *
     * @access    public
     * @param    string
     * @return    string
     */
    function real_url($url)
    {
        return @fsockopen("$url", 80, $errno, $errstr, 30);
    }


Don't forget to explain to visitors that they don't need to append "http://" to the url when you're validating against real_url()!!!
#2

[eluser]Mike Ryan[/eluser]
Hi Iverson,

Great contribution, I have always been a little surprised that this function was not included as a built-in rule. This will be useful on a current project, so thank you.

You included a match for the port specification... nice!

I did find one bug - this regex will match "www.domain.co.u-k".

There's a great online regex tester here. The site also has some great examples of regexs, including some good ones for URLs (neither of which are perfect):

Code:
/(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/\.\?=a-zA-Z0-9_-]+))/igs
/^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))?(:[\d]{1,5})?+([a-zA-Z0-9\/\_\?\.\,\&]+)?/
#3

[eluser]Iverson[/eluser]
Yeah me too. Hopefully the next version will come with it. If it does I'm sure it'll be better than mine. I've updated the code with the better regex.
#4

[eluser]Unknown[/eluser]
Just a quick heads up. "http://en.wikipedia.org" is not considered a valid URL according to the regex tester mentioned by Mike Ryan.
#5

[eluser]hugle[/eluser]
[quote author="Bruno De Barros @ Terra Duo" date="1250696282"]Just a quick heads up. "http://en.wikipedia.org" is not considered a valid URL according to the regex tester mentioned by Mike Ryan.[/quote]

Hello everyone

I wanted to ask, how do I validate url,

so:
http(s)://example.com
http(s)://*.example.com
http(s)://example.com/*
http(s)://www.example.com/*
http(s)://*.example.com/*

As a result I just need to get true/false..
can someone help me?

thanks!
#6

[eluser]Muser[/eluser]
I get the following error:

A PHP Error was encountered

Severity: Warning

Message: preg_match() [function.preg-match]: Unknown modifier '|'

Filename: libraries/MY_Form_validation.php

Line Number: 20
#7

[eluser]hugle[/eluser]
[quote author="Muser" date="1257037344"]I get the following error:

A PHP Error was encountered

Severity: Warning

Message: preg_match() [function.preg-match]: Unknown modifier '|'

Filename: libraries/MY_Form_validation.php

Line Number: 20[/quote]

can you paste exact function/rule?
#8

[eluser]aidehua[/eluser]
[quote author="Mike Ryan" date="1239399749"]Hi Iverson,

Great contribution, I have always been a little surprised that this function was not included as a built-in rule. [/quote]

Look at line 7 in system/language/english/form_validation_lang.php :
Code:
$lang['valid_url']             = "The %s field must contain a valid URL.";

And yet, you're right, I can't see a valid_url() function in the form_validation library. Maybe I'm missing something, or maybe someone was planning to create a valid_url() validation function and, err, forgot? :coolsmirk:
#9

[eluser]aidehua[/eluser]
Since I can't find it built in to the form_validation library, I've created a MY_form_validation library with a valid_url() function in it:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_form_validation extends CI_form_validation {

    function valid_url($str){

           $pattern = "/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";
            if (!preg_match($pattern, $str))
            {
                return FALSE;
            }

            return TRUE;
    }

}


Which I call in my validation rules like this:

Code:
$this->load->helper('url');
$this->form_validation->set_rules('author_url', 'Website', 'trim|prep_url|valid_url|xss_clean');

NB the regex in the valid_url() function requires an http:// or https:// or ftp:// prefix. I've added prep_url to the validation rule to add http:// if it is required. That's why I've loaded the URL helper first.

As expected, when validation fails on the author_url field, CodeIgniter displays this message:

Quote:The Website field must contain a valid URL

Straight out of the form_validation_lang.php file. Weird that it never made it into the form_validation library.
#10

[eluser]Quaze[/eluser]
Just as Aidehua i made a MY_ library.
Only i used the php built in function filter_var

so it gets more clean, and you dont need to prep_url.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_form_validation extends CI_form_validation {

    function valid_url($str){
            if(filter_var($str, FILTER_VALIDATE_URL)){
                return TRUE;
            }
            else{
                return FALSE;
            }
        }
}


EDIT:

Also could be:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_form_validation extends CI_form_validation {

    function valid_url($str){
            return filter_var($str, FILTER_VALIDATE_URL)
            }
}




Theme © iAndrew 2016 - Forum software by © MyBB