Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/12738-form-validation/
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]
Link to comment
https://forums.phpfreaks.com/topic/12738-form-validation/#findComment-48834
Share on other sites

  • 2 weeks later...
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.
Link to comment
https://forums.phpfreaks.com/topic/12738-form-validation/#findComment-53331
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.