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

asked 2021-05-03T11:30:47.543+00:00
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)
{
try
{
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");
        }
        else
        {
           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,314 questions
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2021-05-04T05:12:41.653+00:00
    Viorel 82,371 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.

    No comments