drgmak drgmak - 6 months ago 19
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.

In VB.NET/C#:

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


I want for example to create a variable like below:

C#

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


VB

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


What am i missing?

Answer

You forgot the underscore and the dash:

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