Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,177 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,154 people online right now. Registration is fast and FREE... Join Now!




Problem validating URL

 
Reply to this topicStart new topic

Problem validating URL

jeansymolanza
28 Aug, 2008 - 04:54 PM
Post #1

New D.I.C Head
*

Joined: 20 Feb, 2008
Posts: 34


My Contributions
I am trying to validate URLs uploaded to my database. If the URL field is left empty validation should be skipped. If the URL field has >= 1 character in it then validation should carry on. However the URL's provided are always incorrect despite me adhering to the validation rules during posting. What could be going wrong? Thanking you in advance.

CODE

<?     
            $num_char=strlen($fileurl);
            $num_char2=strlen($youtubeurl);
            
            $url = "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";
            
                 if ( $num_char >= 1 && preg_match($url, $fileurl)) {
      echo "You must supply a file URL in a valid format (for example: <i>http://en.wikipedia.org/wiki/Image:Meissner_effect_p1390048.jpg</i>). Click <a href=\"addtopic.php\">here</a> to start again.<p>";
      exit ();
      } else {;}
      
       if ( $num_char2 >= 1 && !preg_match($url,$youtubeurl)) {
      echo "<div align=\"left\" class=\"style9\">You must supply a YouTube URL in a valid format (for example <i>http://www.youtube.com/watch?v=mG5VFBfHPf0</i>). <p>
            <img src=\"../images/youtubeurl.jpg\"><p>
            Click <a href=\"addtopic.php\">here</a> to start again.<p></div>
            ";
              exit ();
      } else {;}
            ?>



This post has been edited by jeansymolanza: 28 Aug, 2008 - 04:55 PM
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Problem Validating URL
28 Aug, 2008 - 06:21 PM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
php
if ( $num_char >= 1 && preg_match($url, $fileurl))


Did you mean for that to be preg_match or !preg_match?
User is offlineProfile CardPM
+Quote Post

Ridikule
RE: Problem Validating URL
28 Aug, 2008 - 08:01 PM
Post #3

D.I.C Head
**

Joined: 29 Sep, 2005
Posts: 58



Thanked: 4 times
My Contributions
Probably a simpler way to validate the email is by filter.

$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

This function will return the email address if it's a valid email format, and false if it is not. In the above example, I'm assuming there's an 'email' value in $_POST. If you're using GET, then the first parameter of this function should be changed to INPUT_GET. The second parameter is just the name of the form field.
User is offlineProfile CardPM
+Quote Post

jeansymolanza
RE: Problem Validating URL
29 Aug, 2008 - 01:55 AM
Post #4

New D.I.C Head
*

Joined: 20 Feb, 2008
Posts: 34


My Contributions
Turns out it was a typo... lol thanks JackOfAllTrades.

I need to validate email at a later stage so thanks Ridikule.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:41AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month