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;…
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;…
Limit input to letters or numbers and length
Limit 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…