0

I am using the following expression for URL validation.

^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$

However, when I have

"httpcolonforwardslashessite" (colon = : and foward slashes = //)

it still passes it as a url. how can I modify the above expression to make sure that there is ".com/net" at the end?

Thank you

tldr: I want to check for .com or .net at the end of url. What changes can I make to above exp to achieve this?

007
  • 2,136
  • 4
  • 26
  • 46
  • 1
    You might want to look at http://stackoverflow.com/questions/833469/regular-expression-for-url – Carlos Blanco Jun 19 '13 at 15:31
  • thanks...but that got a bit more complicated than I was hoping for...very complex regex. What can I do to add a "clause" at the end that check for .com or .net at the end? I want url with http://google to be rejected – 007 Jun 19 '13 at 15:35

2 Answers2

4

Can't you use inbuilt function, instead of Regex?

Uri.TryCreate Method can help you.

This method

  • returns a Boolean value that is true if the Uri was successfully created; otherwise, false.
  • can check different kinds of urls like Absolute or/and Relative using an overload that accepts UriKind Enumeration as a parameter.
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
1

Assuming you only want .com, this will work:

^(https?://)?([\da-z.-]+).([a-z.]{2,6})([/\w .-])/?\.com([/\w \.!"£$%^&\(\)\*])*

or this one to include not just .com from here

^(http|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
Tom Beech
  • 2,289
  • 1
  • 21
  • 25