Hey

I want to control a textbox to only allow numbers, backspace and a certain length. Ive tried with the IsNumeric function but I cant seem to get it to work.

Thanks.

Dani AI

Generated

Good question, and note that the tag says asp.net:vb.net while replies (like ryan311’s) show WinForms code. The approach differs between WebForms and WinForms, and it is worth doing validation in a way that also covers paste/drag/drop, not just keystrokes.

For ASP.NET WebForms, combine a TextBox MaxLength with a RegularExpressionValidator so you enforce digits-only and the maximum length on both client and server. Example for up to 6 digits:

<asp:TextBox ID="tbDigits" runat="server" MaxLength="6" />
<asp:RegularExpressionValidator ID="revDigits" runat="server"
    ControlToValidate="tbDigits"
    ValidationExpression="^\d{0,6}$"
    ErrorMessage="Digits only (max 6)."
    Display="Dynamic" />

This blocks non-numeric input and rejects pasted non-digits. If you are targeting modern browsers, you can also render an <input type="number"> for nicer mobile keyboards, but do not rely on it alone; keep the validator for robustness. See RegularExpressionValidator and TextBox.MaxLength.

For WinForms, instead of trapping every key as in ryan311’s snippet, prefer controls that are built for numeric input and also handle paste and edge cases: NumericUpDown (set Minimum, Maximum, and optionally ThousandsSeparator) or MaskedTextBox with a mask like 000000 for fixed-length digits. These give you predictable behavior without writing key filtering yourself. See NumericUpDown and MaskedTextBox.

Finally, whichever UI you use, still validate on the server when you process the value. That way you are covered if JavaScript is disabled (WebForms) or if the text is set programmatically (WinForms).

Recommended Answers

All 5 Replies

Hi,

To create a numeric textbox, look here.

To select a certain length, go to the properties of that textbox and set the Maxlength.

commented: agree +13

Just put this code in your keypress event in your textbox

Select Case e.KeyChar
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
                e.Handled = False
            Case Else
                e.Handled = True
        End Select

if you want to change the input length of textbox just click the properties and then change the MaxLength Value.

Just put this code in your keypress event in your textbox

Select Case e.KeyChar
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
                e.Handled = False
            Case Else
                e.Handled = True
        End Select

if you want to change the input length of textbox just click the properties and then change the MaxLength Value.

Nice solution!
Thanks so much! :)

If Textbox1.Text = "" Then
            GoTo ex

        Else
            If IsNumeric(Textbox1.Text) Then
                GoTo ex
            Else
                MsgBox("You cannot add alpha characters, please input the Integer or numbers !", vbCritical)
                Textbox1.Text = ""
                searchback()
            End If
        End If
ex:
        Exit Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.