Jump to content

Archived

This topic is now archived and is closed to further replies.

lpxxfaintxx

Form Validation

Recommended Posts

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

Share this post


Link to post
Share on other sites
this may sound fairly simple, but why don't you just ADD the http:// after words?

you can have an input something like:

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

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) [snapback]387208[/snapback][/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]

Share this post


Link to post
Share on other sites
You could also use something like:

[code]
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)
}
[/code]

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.