Winform: a problem caused by a login form series (2): Restrictions on User Name text box input

Source: Internet
Author: User

When discussing text box input restrictions, we mainly focus on two issues:

1. How do I limit the length of user name input?

A: Set the txtname attribute maxlength = "10"; (Here we use 10 characters as an example)

2. How to restrict the user name from entering only letters, numbers, and underscores

A: There are two ways to achieve this:

Method 1:

// Implemented by listening to keychar on the keyboard
Private void txtname_keypress (Object sender, keypresseventargs E)
{
This.txt name. imemode = imemode. off;

If (E. keychar> = '0' & E. keychar <= '9') | (E. keychar> = 'A' & E. keychar <= 'Z') | (E. keychar> = 'A' & E. keychar <= 'Z') | (E. keychar = 8) | (E. keychar = '_'))
{
E. Handled = false;
}

Else
{
MessageBox. Show ("the user name can only be letters, numbers, and underscores! ");
E. Handled = true;
}

}

 

Method 2:

 

// Use regular expressions to express event matching
Private void txtname_keypress (Object sender, keypresseventargs E)
{
This.txt name. imemode = imemode. off;

RegEx Reg = new RegEx (@ "^ \ W + $ ");

If (this.txt name. Text! = "") // Determines whether to add a null value.
{
If (reg.ismatch(this.txt name. Text ))
{
E. Handled = false;
}

Else
{
MessageBox. Show ("the user name can only be letters, numbers, and underscores! ");
E. Handled = true;
}
}
}

 

The two methods have their own advantages and disadvantages. in actual application, you will find that the first method can limit the characters that do not match the user input before the input, the second method is to make judgments after user input. Some common regular expressions are attached to this article for your convenience:

Only numbers are allowed: "^ [0-9] * $ ".
Only n digits can be entered: "^ \ D {n} $ ".
You can only enter at least N digits: "^ \ D {n,} $ ".
Only M ~ can be input ~ N-digit :. "^ \ D {m, n} $"
Only numbers starting with zero and non-zero can be entered: "^ (0 | [1-9] [0-9] *) $ ".
Only positive numbers with two decimal places can be entered: "^ [0-9] + (. [0-9] {2 })? $ ".
Only 1 ~ Positive number of three decimal places: "^ [0-9] + (. [0-9] {1, 3 })? $ ".
Only a non-zero positive integer can be entered: "^ \ +? [1-9] [0-9] * $ ".
Only a non-zero negative integer can be entered: "^ \-[1-9] [] 0-9" * $.
Only 3 characters can be entered: "^. {3} $ ".
You can only enter a string consisting of 26 English letters: "^ [A-Za-Z] + $ ".
You can only enter a string consisting of 26 uppercase letters: "^ [A-Z] + $ ".
You can only enter a string consisting of 26 lower-case English letters: "^ [A-Z] + $ ".
You can only enter a string consisting of a number and 26 English letters: "^ [A-Za-z0-9] + $ ".
You can only enter a string consisting of digits, 26 English letters, or underscores (_): "^ \ W + $ ".
Verify the User Password: "^ [A-Za-Z] \ W {5, 17} $". The correct format is: start with a letter, with a length of 6 ~ It can only contain characters, numbers, and underscores.
Check whether ^ % & ',; =? $ \ "And other characters:" [^ % & ',; =? $ \ X22] + ".
Only Chinese characters can be entered: "^ [\ u4e00-\ u9fa5] {0,} $"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.