1

I have added a MaskedTextBox control to my form and i wannt users to enter a valid url to control. What mask code i enter to control?

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
Sefa
  • 8,865
  • 10
  • 51
  • 82

1 Answers1

2

you don't really want to mask a url, as I believe masking doesn't support regex, masks are set in stone, there are no wild cards.

for example you may have a mask for: aaa.aaaaaa.aaa

this will only work for something like www.google.com

masks are ideal for situations where you know the fixed length such as a date or zip code.

websites can be change from site to site in length. It would be much better to have a plain textbox on your form and use Regular Expression to validate that it is a website, although that is also not an easy task. as there are many different variations for valid websites.

something like this in regex can check for websites:

^((nntp|sftp|ftp(s)?|http(s)?|gopher|news|file|telnet):\/\/)?(([a-zA-Z0-9\._-]*([a-zA-Z0-9]\.[a-zA-Z0-9])[a-zA-Z]{1,6})|(([0-9]{1,3}\.){3}[0-9]{1,3}))(:\d+)?(\/[^:][^\s]*)?$

more can be found here: http://regexlib.com/Search.aspx?k=URL&c=-1&m=-1&ps=100

Krum110487
  • 611
  • 1
  • 7
  • 20
  • 1
    +1 Good advice on avoiding MaskedTextBox for this. Tho I'd say he should use the `Uri` class to validate the URL, rather than trying to re-implement the logic. See this question for how: http://stackoverflow.com/questions/4835269/how-to-check-that-a-uri-string-is-valid (See: [`Uri.TryParse`](http://msdn.microsoft.com/en-us/library/ms131572.aspx)). – Merlyn Morgan-Graham Sep 11 '11 at 10:45
  • ah yes, I forgot how handy .net is sometimes, they do most of the work for you! – Krum110487 Sep 11 '11 at 10:51