3 Replies - 1540 Views - Last Post: 15 October 2012 - 01:06 PM Rate Topic: -----

#1 maxbre  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 02-October 12

Only allow letters and numbers in a textbox.

Posted 14 October 2012 - 02:06 PM

Hi, how should I make an if statement that check if the textbox contains invaild characters? I'm making a sign up page and don't want the user to have a username like "Us#er!*name".

This is my validation part atm.

            if(string.IsNullOrEmpty(TextBox1.Text))
            {
                Label1.Visible = true;
                Label1.ForeColor = Color.Red;
                Label1.Text = "You need to enter a username.";
                return;
            }
            
            if (string.IsNullOrEmpty(TextBox2.Text))
            {
                Label1.Visible = true;
                Label1.ForeColor = Color.Red;
                Label1.Text = "You need to enter a password.";
                return;
            }

            if (string.IsNullOrEmpty(TextBox3.Text))
            {
                Label1.Visible = true;
                Label1.ForeColor = Color.Red;
                Label1.Text = "Confirm your password.";
                return;
            }

            if (TextBox2.Text != TextBox3.Text)
            {
                Label1.Visible = true;
                Label1.ForeColor = Color.Red;
                Label1.Text = "Passwords does not match.";
                return;
            }



Is This A Good Question/Topic? 0
  • +

Replies To: Only allow letters and numbers in a textbox.

#2 Michael26  Icon User is online

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,421
  • Joined: 08-April 09

Re: Only allow letters and numbers in a textbox.

Posted 14 October 2012 - 04:17 PM

Read about the Keys Enum, you can do a lot of combination with this.
Was This Post Helpful? 0
  • +
  • -

#3 dotINSolution  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 16
  • Joined: 25-September 12

Re: Only allow letters and numbers in a textbox.

Posted 15 October 2012 - 09:31 AM

Using regular expressions, something like Regex.IsMatch("^[a-z0-9_-]{3,16}$", "username here") should work out

if (Regex.IsMatch("^[a-z0-9_-]{3,16}$", TextBox1.Text))
{
    Label1.Visible = true;
    Label1.ForeColor = Color.Red;
    Label1.Text = "Please use a valid name between 3-16 characters";
    return;
}


Was This Post Helpful? 0
  • +
  • -

#4 maxbre  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 02-October 12

Re: Only allow letters and numbers in a textbox.

Posted 15 October 2012 - 01:06 PM

View PostdotINSolution, on 15 October 2012 - 09:31 AM, said:

Using regular expressions, something like Regex.IsMatch("^[a-z0-9_-]{3,16}$", "username here") should work out

if (Regex.IsMatch("^[a-z0-9_-]{3,16}$", TextBox1.Text))
{
    Label1.Visible = true;
    Label1.ForeColor = Color.Red;
    Label1.Text = "Please use a valid name between 3-16 characters";
    return;
}


Worked fine! Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1