1

I'd like to test whether the URL that the user inputs into my form is "proper", e.g. the following are proper:

http://www.google.com
www.google.com
www.google.com/

but the following probably shouldn't be:

google
http://www.go?ogle?#%

I don't have in mind what "proper" means, but is there some standard out there that I can use?

unor
  • 87,575
  • 24
  • 195
  • 335
Paul S.
  • 4,216
  • 10
  • 33
  • 51

1 Answers1

4

In HTML5 you can use the input element with the type value url: http://www.w3.org/TR/html5/states-of-the-type-attribute.html#url-state-type-url. You'd need to check which browsers already implemented a validation for it, though. If it's important, you'd also need server-side validation, of course.

Here you can see what URLs are considered valid by HTML5: http://www.w3.org/TR/html5/urls.html#valid-url. It references RFC 3986 for URIs and RFC 3987 for IRIs.

You should probably have a look at RegEx for URL validation (see for example this question: PHP validation/regex for URL) or check if your library/programming-language/CMS has special functions for it.

Community
  • 1
  • 1
unor
  • 87,575
  • 24
  • 195
  • 335