Unable to give the input for Windows forms in .NET Framework project

Gowri 1 Reputation point

I am creating an application with windows forms checking the credentials. The username needs to be of the format "domain name"\"user".
I am getting an error with the code. How can I correct it

private void textBoxUsername_TextChanged(object sender, EventArgs e)
buttonLogin.Enabled = textBoxUsername.Text != "" && textBoxPassword.Text != "" ? true : false;
TextBox textBox = sender as TextBox;
string userName = textBox.Text;

        string DomainStyleLogin = "@/^[a - zA - Z][a - zA - Z0 - 9 -]{ 1,61}[a-zA-Z]\\.[a-zA-Z]{2,}$/";
        bool isMatched = Regex.IsMatch(userName, DomainStyleLogin);
        if (!isMatched)
           MessageBox.Show("Invalid Login Format");
           MessageBox.Show("Login Successfully");
     catch(Exception ex)
        MessageBox.Show("Something went wrong: " + ex.ToString());
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,797 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 109.8K Reputation points

    Try something like this:

    string DomainStyleLogin = @"^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\\[a-zA-Z]{2,}$";  

    (Taking into consideration https://learn.microsoft.com/en-us/answers/questions/380789/).

    It can be further improved if you give more details about the rules and sample inputs.

    0 comments No comments