简体   繁体   中英

codeigniter form validation rules url

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?

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

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 .

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

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

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

you can use following code or by updating validation library visit: https://brianistech.wordpress.com/2010/11/19/url-validation-in-codeigniter/

$pattern = "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";

if (!preg_match($pattern, $_REQUEST['test'])){
    echo 'The URL you entered is not correctly formatted.';        
 }

In codeigniter you can use valid_url callback to check if the entered URL is valid or not

For example: A field with name "url" having label "Remote Url" can be validated using this code

$this->form_validation->set_rules('url', 'Remote Url', 'callback_valid_url');

And add a new function

 public function valid_url($str) {
        return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM