-2

I'm using below regex for url validation.

 public static readonly string UrlValidation = @"^((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$";

But,if i input google..com.my, the result is valid, but actually is not. How to amend the regex to make it not valid if input 2 dots.

nettium
  • 67
  • 2
  • 3
  • 9

3 Answers3

3

A regex is probably the wrong approach here. Instead, consider Uri.IsWellFormedUriString. See documentation here. Be warned, however--this function validates a wide variety of URIs.

Jack
  • 396
  • 2
  • 18
1

This Url Regex string comparison webpage has proven very helpful to me:

https://mathiasbynens.be/demo/url-regex

The @diegoperini (502 chars) regex string proved infallible through all the tests in the above link:

_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS
Lucas Niewohner
  • 327
  • 3
  • 11
0

Try foolowing regex

 /(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?/

Reference : codeproject

Alsamil Mehboob
  • 384
  • 2
  • 6
  • 24