Jump to content


Photo

Form Validation


  • Please log in to reply
2 replies to this topic

#1 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 23 June 2006 - 04:04 PM

Hello,

As I am close to launching my tutorial site, I almost forgot something very important. Form Validation. I got most of them to work, except for one major one, the URL. I want the user to enter the url with "http://" in the front. Unfortunately, a lot of users were submitting URL's with "www." in front, instead of "http://." That is bad, because it would then link to "www.mysite.com/page.phpwww.tutorialurl.com/tut1.php" etc.

So far, I tried to do the following:

if (!eregi ('^((http|https|ftp)://)?([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_\.~?\-]*)$', stripslashes(trim($_POST['url'])))) {
$problem = TRUE;
$message .= '<p>Please enter a valid URL.</p>';
}

It did not work for the "http://" part, but it did work for the ".com" part..

Does anyone know a way to require a "http://" in front of the post?

Thanks

#2 jworisek

jworisek
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 23 June 2006 - 04:13 PM

this may sound fairly simple, but why don't you just ADD the http:// after words?

you can have an input something like:

http:// <input type="text" name="url" />
//  or simply just put http:// already in the input
<input type="text" name="url" value="http://" />

It just seems kinda silly and probably very frustrating for the users to have to put the http:// in themselves.

[!--quoteo(post=387208:date=Jun 23 2006, 11:04 AM:name=lpxxfaintxx)--][div class=\'quotetop\']QUOTE(lpxxfaintxx @ Jun 23 2006, 11:04 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hello,

As I am close to launching my tutorial site, I almost forgot something very important. Form Validation. I got most of them to work, except for one major one, the URL. I want the user to enter the url with "http://" in the front. Unfortunately, a lot of users were submitting URL's with "www." in front, instead of "http://." That is bad, because it would then link to "www.mysite.com/page.phpwww.tutorialurl.com/tut1.php" etc.

So far, I tried to do the following:

if (!eregi ('^((http|https|ftp)://)?([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_\.~?\-]*)$', stripslashes(trim($_POST['url'])))) {
$problem = TRUE;
$message .= '<p>Please enter a valid URL.</p>';
}

It did not work for the "http://" part, but it did work for the ".com" part..

Does anyone know a way to require a "http://" in front of the post?

Thanks
[/quote]


#3 dwees

dwees
  • Members
  • PipPipPip
  • Advanced Member
  • 47 posts
  • LocationUnited Kingdom

Posted 05 July 2006 - 02:11 PM

You could also use something like:

function  validate_url($entered_url) {
   $length = str_length($entered_url);
   for ($i = 1; $i <= $length; $i++) {
       $write_string .= $entered_url{i$};
       if ($write_string == "http://"){
          return (true);
       }
   }
   return(false)
}

This is the brute force way of doing it though, but at least it's useable in any other spot you want to use the code.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users