Restrict numbers and letters in textbox - C#

前端 未结 6 1606
心在旅途
心在旅途 2021-01-24 10:35

I want to restrict what numbers and letters can be entered into a textbox. Let\'s say I only want to allow numbers 0-5 and letters a-d (both lower and uppercase). I already tri

6条回答
  •  野性不改
    2021-01-24 11:13

    Use the KeyPress Event for your textbox.

    protected void myTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs)
    {
        e.Handled = !IsValidCharacter(e.KeyChar);
    }
    
    private bool IsValidCharacter(char c)
    {
        bool isValid = true;
    
        // put your logic here to define which characters are valid
        return isValid;
    }
    

提交回复
热议问题