Validate URL in form

Hello Sitepoint

I’ve created a form that should validate required fields, URL and email - Problem is the URL… I’ve used this code for the URL:

if ($_POST['homepage'] != "") {  
				            $homepage = filter_var($_POST['homepage'], FILTER_SANITIZE_URL);  
				            if (!filter_var($homepage, FILTER_VALIDATE_URL)) {  
				                $errors .= "$homepage is <strong>NOT</strong> a valid URL.<br/><br/>";  
				        } else {  
				            $errors .= 'Please enter your home page.<br/>';  

Problem is that FILTER_SANITIZE_URL require to use “http://” in front of the email address…

And I want to be able to write etc: “www.domainname.tld” instead of “http://domainname.tld” - I don’t really know how to use regex or create them… But anyone that can help?

Thank you alot!

// if they typed something in the homepage box but it doesn't start with
// http:// or https://, add http:// before what they typed
if (!empty($_POST['homepage']) && strpos($_POST['homepage'], 'http://') === false && strpos($_POST['homepage'], 'https://') === false) {
    $_POST['homepage'] = 'http://' . $_POST['homepage'];
// now do the rest of your validation code

Thank you a lot! Sorry for first seeing it now, I’ve been away from my computer some days, and too many mail, just saw this now!

And thank you! It’s working :smiley: