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

Restrict input to letters or numbers and length
In the daily development process, verifying the legitimacy of characters has always been an essential step. In the past, the judgment was made after the user input. What about restrictions on input?

In the daily development process, verifying the legitimacy of characters has always been an essential step. In the past, the judgment was made after the user entered the input. , why not limit it at the time of input?

Taking the TextBox control of Winform as an example, here are two solutions:

  • Match by character
  • regular expressions

Restrict when the user enters, so choose 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 code above uses the KeyPress event handler of the TextBox. In this event, use the char.IsLetterOrDigit method to determine whether the input character is a letter or a number, if not, it will be disposed of and no input is allowed. At the same time, use the char.IsControl method to determine whether the input character is a control character, such as backspace key, delete key, etc. These special keys are allowed to be input. In addition, use the Text attribute of TextBox to get the length of the text in the input box. If the length exceeds 8 characters and the input character is not a control character, it will also be processed. Characters exceeding 8 characters are allowed.

During the test, I found that there is a flaw in this judgment, that is, Chinese can still be input 🙄

regular expression

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

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

   // Limit input length to 8 characters
   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 allowed to be input, which means that only letters (including uppercase and lowercase) are allowed to be input and numeric characters. Use the Regex.IsMatch method to determine whether the input character conforms to this character pattern, if not and it is not a control character (such as Backspace, Delete etc.), then pass e.Handled = true to prohibit input.

In this way, if the user tries to enter a space, Chinese or other characters that do not conform to the character pattern, the input will be prohibited. You don’t need to judge every time🐰, remember to quote the required namespace in advance.

using System.Text.RegularExpressions;
 

One 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/

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
首页
微信
电话
搜索