Valid url check in form validation |
[eluser]Iverson[/eluser]
Here's a little something you can add into the Form_validation.php to check urls. One just checks the syntax. The other actually checks to see if the website exists. Feel free to improve the regex as I'm by no means claiming to be an expert! ![]() Code: // -------------------------------------------------------------------- Don't forget to explain to visitors that they don't need to append "http://" to the url when you're validating against real_url()!!!
[eluser]Mike Ryan[/eluser]
Hi Iverson, Great contribution, I have always been a little surprised that this function was not included as a built-in rule. This will be useful on a current project, so thank you. You included a match for the port specification... nice! I did find one bug - this regex will match "www.domain.co.u-k". There's a great online regex tester here. The site also has some great examples of regexs, including some good ones for URLs (neither of which are perfect): Code: /(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/\.\?=a-zA-Z0-9_-]+))/igs
[eluser]Iverson[/eluser]
Yeah me too. Hopefully the next version will come with it. If it does I'm sure it'll be better than mine. I've updated the code with the better regex.
[eluser]Unknown[/eluser]
Just a quick heads up. "http://en.wikipedia.org" is not considered a valid URL according to the regex tester mentioned by Mike Ryan.
[eluser]hugle[/eluser]
[quote author="Bruno De Barros @ Terra Duo" date="1250696282"]Just a quick heads up. "http://en.wikipedia.org" is not considered a valid URL according to the regex tester mentioned by Mike Ryan.[/quote] Hello everyone I wanted to ask, how do I validate url, so: http(s)://example.com http(s)://*.example.com http(s)://example.com/* http(s)://www.example.com/* http(s)://*.example.com/* As a result I just need to get true/false.. can someone help me? thanks!
[eluser]Muser[/eluser]
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
[eluser]hugle[/eluser]
[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?
[eluser]aidehua[/eluser]
[quote author="Mike Ryan" date="1239399749"]Hi Iverson, Great contribution, I have always been a little surprised that this function was not included as a built-in rule. [/quote] Look at line 7 in system/language/english/form_validation_lang.php : Code: $lang['valid_url'] = "The %s field must contain a valid URL."; And yet, you're right, I can't see a valid_url() function in the form_validation library. Maybe I'm missing something, or maybe someone was planning to create a valid_url() validation function and, err, forgot? :coolsmirk:
[eluser]aidehua[/eluser]
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'); Which I call in my validation rules like this: Code: $this->load->helper('url'); 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.
[eluser]Quaze[/eluser]
Just as Aidehua i made a MY_ library. Only i used the php built in function filter_var so it gets more clean, and you dont need to prep_url. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); EDIT: Also could be: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Welcome Guest, Not a member yet? Register Sign In |