1024programmer Asp.Net Limit input to letters or numbers and length

Limit input to letters or numbers and length

Limit input to letters or numbers and length

Limit input to letters or numbers and length
In the daily development process, verifying the legality of characters has always been an indispensable step. In the past, the judgment was made after the user input. Not only was it troublesome, but it also prompted the user to modify it when it did not meet the standards. The experience was very poor, why not What about restrictions when typing?

In the daily development process, verifying the legality of characters has always been an indispensable step. In the past, the judgment was made after the user had finished inputting. Not only was it troublesome, but it also prompted the user to modify it when it did not meet the standards. The experience was very poor. , why not restrict it when inputting?

Take Winform‘s TextBox control as an example. Here are two solutions:

  • Match by character
  • Regular expression

Limit when the user inputs, so select the KeyPress event.

Character match

private void txt_Address_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
 {
   //Only letters and numbers are allowed
   if (!char.IsLetterOrDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '\b')
   {
     e.Handled = true;
   }

   //Limit input length to 8 bits
   if (txt_Address.Text.Length >= 8 && !char.IsControl(e.KeyChar))
   {
     e.Handled = true;
   }
 }
 

The above code uses the TextBox‘s KeyPress event handler. In this event, use the char.IsLetterOrDigit method to determine whether the entered character is a letter or number. If not, it will be processed and input will not be allowed. At the same time, use the char.IsControl method to determine whether the entered character is a control character, such as the backspace key, delete key, etc. These special keys are allowed to be input. In addition, use the Text attribute of TextBox to get the text length in the input box. If the length exceeds 8 characters and the entered character is not a control character, it will also be processed. No More than 8 characters are allowed.

During the testing process, I found that there is a flaw in this judgment, that is, Chinese can still be entered🙄

regular expression

private void txt_Address_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
 {
   // Use regular expressions to define the character patterns that are allowed to be entered
   string pattern = @"[a-zA-Z0-9]";
   Regex regex = new Regex(pattern);

   // Determine whether the entered characters match the allowed character pattern
   if (!regex.IsMatch(e.KeyChar.ToString()) && !char.IsControl(e.KeyChar))
   {
     // If the entered characters do not conform to the character pattern and are not control characters (such as Backspace, Delete, etc.), input is prohibited
     e.Handled = true;
   }

   //Limit input length to 8 bits
   if (txt_Address.Text.Length >= 8 && !char.IsControl(e.KeyChar))
   {
     e.Handled = true;
   }
 }
 

In the above code, we use the regular expression [a-zA-Z0-9] to define the character pattern that is allowed to be entered, which means that only letters (including upper and lower case) are allowed to be entered. and numeric characters. Use the Regex.IsMatch method to determine whether the input character matches this character pattern. If it does not match and is not a control character (such as Backspace, Delete etc.), then pass e.Handled = true to prohibit input.

In this way, if the user attempts to enter spaces, Chinese or other characters that do not conform to the character pattern during input, the input will be prohibited. You don’t have to judge every time🐰. Remember to quote the required namespace in advance.

using System.Text.RegularExpressions;
 

A little lazy trick every day is 👣

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/limit-input-to-letters-or-numbers-and-length-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索