-2

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?

drgmak
  • 935
  • 7
  • 12

3 Answers3

2

Try this:

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

Edit:

Since you're trying to validate domain names a better solution would be to use the solution described here:

// https://stackoverflow.com/a/967610/162671
private static bool IsValidDomainName(string name)
{
    return Uri.CheckHostName(name) != UriHostNameType.Unknown;
}

Or something like this:

var valid = false;
try
{
    var uri = new Uri("http://example.com/");
    valid = false;
}
catch (UriFormatException){
    valid = false;
}
Community
  • 1
  • 1
Nasreddine
  • 33,475
  • 17
  • 73
  • 91
1

You forgot the underscore and the dash:

bool isValid = Regex.IsMatch(txtCreateDomain.Text, "^[a-zA-Z0-9_.-]*$")
eol
  • 15,597
  • 4
  • 25
  • 43
  • This will check only if the text contains all of these right? From my Regex i want all this cases to be valid: domain.intern , domain1.intern, my-domain.intern – drgmak May 31 '16 at 13:03
  • All of these will match, yes. But if you want to validate domain names maybe have a look at: http://stackoverflow.com/questions/24730805/regex-to-match-a-capture-a-domain-name or https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch08s15.html – eol May 31 '16 at 13:06
  • Thanx, this solved my issue. :) – drgmak May 31 '16 at 13:36
  • `Regex.IsMatch(txtCreateDomain.ToString(), @"^[\w.-]*$", RegexOptions.ECMAScript)` is simpler, I think. In VB.NET, the `@` should be removed of course. Also, there is no dash in the pattern, only a *hyphen*. BTW, there is no requirement to match a hyphen in the question. That looks suspicious. – Wiktor Stribiżew May 31 '16 at 13:39
  • @WiktorStribiżew Of course `\w` is simpler, I decided to keep it anyway. Thanks for pointing out that it's a hyphen, not a dash, I wasn't aware of that. Regarding matching the hypen: it's not mentioned in the question, but it's mentioned in the comments for my answer and Nasreddine's answer... – eol May 31 '16 at 13:55
  • 1
    Good, still, the question should contain all the details required to answer it. I do not find fault with your answer, just the question itself. – Wiktor Stribiżew May 31 '16 at 13:58
  • 1
    I am sorry for the mistakes in the question. I appreciaty your help. Thanx for your time – drgmak May 31 '16 at 14:46
0
/^\w+$/

\w is equivalent to [A-Za-z0-9_]
Om Prakash
  • 605
  • 5
  • 13