lpxxfaintxx Posted June 23, 2006 Share Posted June 23, 2006 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 More sharing options...
jworisek Posted June 23, 2006 Share Posted June 23, 2006 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 More sharing options...
dwees Posted July 5, 2006 Share Posted July 5, 2006 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.