[eluser]ghprod[/eluser]
[quote author="aidehua" date="1264721787"]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.[/quote]
Its work perfectly for me
i've modified more simple
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_form_validation extends CI_form_validation {
function valid_url($url)
{
$pattern = "/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";
return (bool) preg_match($pattern, $url);
}
}
regards and thanks