Péter
Péter

Reputation: 2181

Validation regular expression for windows domain user name

I dug through lots of google matches but I can't find a correct, working regular expression to validate domain\username.
I have too little knowledge about regex and I know nothing about the rules of the domain and user name restrictions/rules.

Thanks,
Péter

Upvotes: 1

Views: 15353

Answers (2)

bgh
bgh

Reputation: 2160

I ended up using

/^[a-zA-Z][a-zA-Z0-9\-\. ]{0,61}[a-zA-Z]\\\w[\w\.\- ]+$/

(based on this SO answer)

Upvotes: 2

X3074861X
X3074861X

Reputation: 3819

You could just check for the presence of the backslash or forward-slash in the username.

string UsernameEntered = @"sm/asd";
var DomainStyleLogin = new Regex(@"^.*(\\|/)");
var match = DomainStyleLogin.Match(UsernameEntered);
if (!match.Success)
{
    //Does not contain a backslash
}

EDIT

If you want to check the username or domain entered, you could use something like this :

var ValidUsernameOrDomain = new Regex(@"^[A-Za-z0-9\\\._-]{7,}$");

This will validate for :

  • Match upper and lower case letters, numbers 0-9, underscore, hyphen, backslash, and period.
  • String is not less than 7 characters (Minimum of 3 characters for username and domain, plus the domain slash)

Upvotes: 4

Related Questions