Welcome Guest, Not a member yet? Register   Sign In
Form validation url-check
#1

[eluser]moodh[/eluser]
I just looked through the library, and noticed there's no valid_url method, which in itself is completly fine, I'll just use a regexp check, however:

I then noticed a line in the language files related to form validation, where there indeed exist a line related to url-checking, so how is it, is there an url-check or not? =p

I don't know whether this is a bug or not, so I posted it here. =)
#2

[eluser]kirrie[/eluser]
check this out.

in system/application/config/config.php
Code:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
#3

[eluser]manilodisan[/eluser]
We use this one

Code:
function valid_url($str)
{
    return ( ! preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $str)) ? FALSE : TRUE;
}
#4

[eluser]moodh[/eluser]
[quote author="kirrie" date="1227757124"]check this out.

in system/application/config/config.php
Code:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
[/quote]

Please read my post again, just because CI protects against bad url's doesn't mean form_validation protects user-inputed url, which is what I'm asking about. =)

[quote author="manilodisan" date="1227764622"]We use this one

Code:
function valid_url($str)
{
    return ( ! preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $str)) ? FALSE : TRUE;
}
[/quote]

As a callback I guess? or did you extend the library (form_validation)?
#5

[eluser]djalilk[/eluser]
Hi,

If you are using PHP5 simply add this function to your controller

Code:
function valid_urls($str) {

if(!filter_var($str, FILTER_VALIDATE_URL))

  {

$this->validation->set_message('valid_urls', 'Your URL is incorrect');
return FALSE;
  }

else {
return TRUE;
     }
}

And call it in your validation rules :

Code:
$rules['link'] = "callback_valid_urls";

Hope this is useful
#6

[eluser]Khoa[/eluser]
We can extend the Form_Validation class to integrate this validation into the core class so that we can just use it as any other validation rules.

By combining the reg exp from manilodisan and with the help of how to extend a form validation at this blog http://www.scottnelle.com/41/extending-c...n-library/, I created this extension to the Form_Validation to include the valid_url() function.

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

/**
* MY_Form_Validation Class
*
* Extends Validation library
*
* Adds one validation rule, valid_url to check if a field contains valid url
*/

class MY_Form_Validation extends CI_Form_validation {

    function MY_Form_Validation()
    {
        parent::CI_Form_validation();
    }

    // --------------------------------------------------------------------

    /**
     * valid_url
     *
     * @access    public
     * @param    field
     * @return    bool
     */
    function valid_url($field)
    {
        $CI =& get_instance();
        
        $CI->form_validation->set_message('valid_url', 'The %s field must contain a valid url.');

        return ( ! preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $field)) ? FALSE : TRUE;
    }
}
?>

For those who are new to extending the core libraries (including myself), you need to save this file at: system\application\libraries\MY_Form_Validation.php (make sure you name the file name and its case correctly). Then you can just use it as normal, CI will cleverly pick it up.

Code:
$rules = array(
    array
    (
        'field' => 'websiteURL',
        'label'    => 'Website',
        'rules'    => 'required|max_length[255]|valid_url'
    ),
);

$this->load->library('form_validation');
$this->form_validation->set_rules($rules);

if ($this->form_validation->run())
{
    //...
}
else
{
    //...
}

Hope this helps.

Khoa
#7

[eluser]Dregond Rahl[/eluser]
I was just coding something that needed this, and just wnated to ask if "FILTER_VALIDATE_URL" was better than using RegExp ?
#8

[eluser]Khoa[/eluser]
Dregond Rahl, I'm very new to PHP and CI itself so not very sure what that FILTER_VALIDATE_URL does and whether it is better than RegExp or not. Can you explain a little bit more or maybe give a quick comparison of how to write codes in both ways? We may then evaluate them together.

It's not CI thingy right? I cannot see that term anywhere in the framework.
#9

[eluser]Dregond Rahl[/eluser]
Its part of PHP 5, and form what i tested, RegExp method is better, using the PHP 5 filter it even allowed URLS without a domain.
#10

[eluser]Thorpe Obazee[/eluser]
@Dregond Rahl, try using this.

Code:
function valid_url($url)
{
    return (bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);
}




Theme © iAndrew 2016 - Forum software by © MyBB