Part of the EllisLab Network
   
2 of 2
2
Valid url check in form validation
Posted: 16 November 2010 05:45 AM   [ Ignore ]   [ # 11 ]  
Grad Student
Rank
Total Posts:  42
Joined  12-16-2009
aidehua - 28 January 2010 06:36 PM

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:

<?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:

$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:

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.

Its work perfectly for me smile
i’ve modified more simple

<?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

Profile
 
 
Posted: 25 November 2010 04:22 AM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  169
Joined  04-15-2010

My solution: I don’t try to check everything.

In my validation rule I simply check for http:// or https:// and for spaces in the url:

function valid_url($url){       

  $pattern 
'/' '^(https?:\/\/)[^\s]+$' '/';
    
  
preg_match($pattern$url$matches);
    
  
$CI =& get_instance();
  
$CI->form_validation->set_message('valid_url'"The url must start with 'http://' or contain no spaces");      
    
  return (empty(
$matches)) ? FALSE TRUE;

At some point, checking everything becomes a pain.

Profile
 
 
Posted: 18 January 2011 02:14 PM   [ Ignore ]   [ # 13 ]  
Grad Student
Avatar
Rank
Total Posts:  42
Joined  10-29-2009

Here is an awesome regex to validate a URL with the added side-benefit of finding the parts of the URL.

\b((?#protocol)https?|ftp)://((?#domain)[-A-Z0-9.]+)((?#file)/[-A-Z0-9+&@#/%=~_|!:,.;]*)?((?#parameters)\?[A-Z0-9+&@#/%=~_|!:,.;]*)? 

It is a built-in regex in a program called RegexBuddy by Just Great Software. That guy is a regex guru. I **highly** recommend it for testing regex expressions in C, PHP, Perl, Java and others.


This one lets you also validate relative URL’s

^
(
# Scheme
 
[a-z][a-z0-9+\-.]*:
 (
# Authority & path
  //
  
([a-z0-9\-._~%!$&'()*+,;=]+@)?              # User
  ([a-z0-9\-._~%]+                            # Named host
  |\[[a-f0-9:.]+\]                            # IPv6 host
  |\[v[a-f0-9][a-z0-9\-._~%!$&'
()*+,;=:]+\])  # IPvFuture host
  
(:[0-9]+)?                                  # Port
  
(/[a-z0-9\-._~%!$&'()*+,;=:@]+)*/?          # Path
 |# Path without authority
  (/?[a-z0-9\-._~%!$&'
()*+,;=:@]+(/[a-z0-9\-._~%!$&'()*+,;=:@]+)*/?)?
 )
|# Relative URL (no scheme or authority)
 ([a-z0-9\-._~%!$&'
()*+,;=@]+(/[a-z0-9\-._~%!$&'()*+,;=:@]+)*/?  # Relative path
 |(/[a-z0-9\-._~%!$&'
()*+,;=:@]+)+/?)                            # Absolute path
)
# Query
(\?[a-z0-9\-._~%!$&'()*+,;=:@/?]*)?
# Fragment
(\#[a-z0-9\-._~%!$&'
()*+,;=:@/?]*)?
Profile
 
 
Posted: 26 January 2011 02:28 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  30
Joined  01-09-2009
hugle - 01 November 2009 09:29 AM
Muser - 31 October 2009 08:02 PM

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

can you paste exact function/rule?

I believe you tried to validate an empty string…  You can add this line to the top of the function to check for that:

if( empty($url) ) {
return FALSE;
}

 Signature 

- Charles Garrison

Profile
 
 
   
2 of 2
2