codeigniter form validation rules url

18,244

Solution 1

You can add this to your Form_validation.php file to check url syntax, or even check if the actual URL exists.

/**
 * 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);
} 

Solution 2

As of Codeigniter 3, there is a new form validation rule for valid_url that you could use. This will check for the correct syntax, but not check if you can actually hit the url. You would have to use the custom real_url function in this answer.

Documentation for the new validation rule is found here.

Solution 3

Add the following simple code in your form_validation.php file:

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

Solution 4

I don't believe that CI has a validation method for urls. What you can do is look up this resource: http://www.codeigniter.com/user_guide/libraries/form_validation.html and create your own using things such as filter_var, filter_input and using the validation for a url: http://php.net/manual/en/filter.filters.validate.php

Solution 5

to correct/ add to bitwise creative's answer, it returns a resource, not TRUE/FALSE

You should also change your application/libraries/MY_Form_validation.php file. Not overwriting the code igniter library.

function real_url($url)
{
    $port = 80;
    $url_to_test = $url;
    if (substr($url_to_test,0,7) == 'http://')
    {
        $url_to_test = substr($url_to_test,7);  
    }
    else if (substr($url_to_test,0,8) == 'https://')
    {
        $url_to_test = substr($url_to_test,8);
        $port = 443;
    }
    $r= @fsockopen($url_to_test, $port, $errno, $errstr, 5);
    return is_resource($r);
} 
Share:
18,244
DonOfDen
Author by

DonOfDen

ಠ_ಠ www.DonOfDen.com You can contact me via javascript:atob("YXJhdmluZGt1bWFyLmdhbmVzYW5AZ21haWwuY29t");

Updated on June 09, 2022

Comments

  • DonOfDen
    DonOfDen almost 2 years

    How can I validate form field which has URL as input value.

    I tried with following rules. But it allows the field if the user input a string.

    View File:

    <?php $data = array(
                        'name'        => 'com_url',
                        'id'          => 'com_url',
                        'value'       => set_value('com_url'),
                        'placeholder' => $this->lang->line('register_com_url'),
                        'class'       => 'form-control',
                        'maxlength'  => 100
    
                      );
                      echo form_input($data);
                ?>
    

    Config:

    array(
        'field'   => 'com_url',
        'label'   => 'Com URL',
        'rules'   => 'trim|required|prep_url'
    )
    

    I user prep_url but it adds just http:// in the field but didn't validate the field for URL.

    My Field should accept only the following formats:

    http://www.sample.com

    www.sample.com

    http://sample.com

    How can I do this?

  • DonOfDen
    DonOfDen about 9 years
    @BitwiseCreative I used callback and the function in controller.. A same as u did..