How to prevent a script under "_" from being added to the end of the contents of a TextBox

2

I'm working with nicks (first names).

When a user registers, he must enter his nickname, he can not contain symbols (except the underscore), only numbers and letters.

I use the event KeyPress of my TextBox Username for this:

private void Username_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsLetterOrDigit(e.KeyChar)) this.Handled = false;
    else
    {
        if (e.KeyChar == '\b') this.Handled = false; //Tecla de borrado
        else
        {
           if (e.KeyChar == '_' && !((TextBox)sender).Text.Contains("_") && ((TextBox)sender).Text.Length > 0) this.Handled = false;
            else this.Handled = true;
        }
    }

    e.Handled = Handled;
}

This piece of code prevents symbols (other than "_") from being written, content starting with "_" and using more than one script under "H_O_L_A", but I need to prevent the underscore from being can use at the end, that is:

Allow: Hol_a

Prevent: Hello _

How can I get this?

    
asked by Héctor Manuel Martinez Durán 02.05.2018 в 00:57
source

2 answers

1

Here is a possible solution:

public partial class Form1 : Form {
    private string text = string.Empty;

    public Form1() {
        InitializeComponent();
        this.textBox1.KeyDown += TextBox1OnKeyDown;
    }

    private void TextBox1OnKeyDown(object sender, KeyEventArgs e) {
        var ultimoCaracter = ((char)e.KeyCode).ToString();
        var fullText = textBox1.Text + ultimoCaracter;

        if (!CumpleReglaNegocio(fullText)) {
            e.SuppressKeyPress = true;
        }
    }

    private bool CumpleReglaNegocio(string fullText) {
        return new Regex(@"^[a-zA-Z|\d]+$").IsMatch(fullText);
    }
}

However, handling the "keydown" event can only be useful if your system is not internationalized in another language whose character format is different from ours, for example Chinese, in which you can press 2 or more keys to represent a single character. If this is not your case, this code can work for you.

If you want a solution that meets the case I mentioned above, I would recommend handling it with the "TextChanged" event and define a local variable that saves the state prior to the change to set the textbox with the value of this variable, in case the text entered does not comply with your business rule.

I hope it has helped you!

    
answered by 02.05.2018 / 16:48
source
1

What can help you validate if the last value of the string is the script is to add extra validation to your code. I think you can solve the problem with the following code:

private void Username_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsLetterOrDigit(e.KeyChar)) this.Handled = false;
    else
    {
        if (e.KeyChar == '\b') this.Handled = false; //Tecla de borrado
        else
        {
           string senderText = (TextBox)sender).Text;
           if (e.KeyChar == '_' 
           && !(senderText.Contains("_") 
               && (senderText.Length > 0
               && (senderText[senderText.Length - 1] != "_")
           ) this.Handled = false;
            else this.Handled = true;
        }
    }

    e.Handled = Handled;
}
    
answered by 02.05.2018 в 05:28