drgmak drgmak - 1 year ago 65
Vb.net Question

Regex does not work properly

I have a windows form, in which i have a textbox. I want to check using Regex that the text inserted does not contain any other character except:

  1. Alphabetic letters [a-z] or [A-Z]

  2. Numbers, 0-9

  3. _ (underscore) or . (Dot)

I have written this line of code which seems to not work properly.


Regex.IsMatch(txtCreateDomain.ToString(), "^[a-zA-Z0-9_.]*$")

I want for example to create a variable like below:


bool isValid = Regex.IsMatch(txtCreateDomain.ToString(), "^[a-zA-Z0-9_.]*$")


Dim isValid As Boolean = Regex.IsMatch(txtCreateDomain.ToString(), "^[a-zA-Z0-9_.]*$")

What am i missing?

Answer Source

You forgot the underscore and the dash:

bool isValid = Regex.IsMatch(txtCreateDomain.Text, "^[a-zA-Z0-9_.-]*$")