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

[eluser]WanWizard[/eluser]
Not a good idea.

First of all, filter_var() is PHP 5.2.0+, which is not always available. And second, it does a very lousy job in validating a URL. Internally, it only checks if a parse_url() is possible, for with the PHP documentation states that it should not be used for URL validation.
#12

[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 Smile
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
#13

[eluser]nuwanda[/eluser]
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:

Code:
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.
#14

[eluser]mrbinky3000[/eluser]
Here is an awesome regex to validate a URL with the added side-benefit of finding the parts of the URL.

Code:
\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

Code:
^
(# 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\-._~%!$&'()*+,;=:@/?]*)?
$
#15

[eluser]Charles Garrison[/eluser]
[quote author="hugle" date="1257085769"][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?[/quote]

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;
}




Theme © iAndrew 2016 - Forum software by © MyBB