0

I've got an input where the user is able to enter their website URL.

I can't use FILTER_VALIDATE_URL because that requires that the user types in http:// before their URL. Some users might just type www.website.com or website.com.

How can I validate a URL with the following rules:

  • doesn't require the user to have http://, https://, or any other form of this
  • doesn't require the user to have www. before their link (example: just website.com)
  • doesn't matter what domain extension is used
  • allows links like www.website.com/username or anything alike to be used

How would this be done?

Bagwell
  • 2,018
  • 5
  • 18
  • 24
  • Rules vary from post to post. – Bagwell Nov 09 '13 at 05:59
  • so... read the posts and pick which one works best for you. I had this exact problem two weeks ago. I had to use a combination of things. There's a _million_ answers to choose from. It just takes a little research. Anyone who answers this for you will just be doing the research for you. – gloomy.penguin Nov 09 '13 at 06:00
  • 1
    I haven't found one that fits what I need. – Bagwell Nov 09 '13 at 06:00
  • I've found something that's almost what I need. How would I change this so that it accepts any domain extension rather than just .com, .net, etc? `$pattern = '/(?:https?:\/\/)?(?:[a-zA-Z0-9.-]+?\.(?:com|net|org|gov|edu|mil)|\d+\.\d+\.\d+\.\d+)/';` – Bagwell Nov 09 '13 at 06:08

1 Answers1

0

Use this one

/(http[s]?:\/\/)?(www[\w]?\.)?[\w]*\.[\w]{2,3}[\/[\w]*]*$/i

this regular expression fits the following creteria:

  1. http:// or https:// are optional but if user mentioned them, should be written in write way
  2. www(.). is optional but if mentioned should be in right way
  3. some parameters after the url are allowed. If you want just a devision after url, I mean www.facebook/user convert * at the end of above expression to ?
Iraj Hedayati
  • 1,478
  • 17
  • 23