Tuesday, October 1, 2013

CodeIgniter lacks with URL validation. The only one that it provides is called prep_url which does nothing more then prepend "http://" to URLs if missing.

Create a file in the application/library directory called MY_Form_validation.php. Make the class name MY_Form_validation as well. Be sure that it extends CI_Form_validation, and that it calls the parent constructor. This will allow you to extend the built-in validation library and add additional validators to it.
Then in your code, you can simply add additional validators like:

$rules['URL'] = 'required|prep_url|valid_url_format|url_exists';

The two new validators are valid_url_format which checks for a properly formatted URL and url_exists which checks for a valid server. You should prefix the two with prep_url so that if they don’t enter a scheme the other validators won’t fail.