Only numeric input allowed in a textbox

I'm writing a small application. The only input needed are some numeric values. Can anyone tell me which code I have to write to manage that only numeric input is allowed in a text box.
The solution which I tried is generating a popup when someone uses a string in stead of numeric characters. But till now I can't get it working. The code i've tried is:

If Not TextBox1.Text = Nothing Then

If Asc(TextBox1.Text) < 47 Then
If Asc(TextBox1.Text) > 58 Then
MessageBox.Show("Only numeric input is allowed", , MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End If

Thanks in advance,

Ernesto



Comments

  • : I'm writing a small application. The only input needed are some numeric values. Can anyone tell me which code I have to write to manage that only numeric input is allowed in a text box.
    : The solution which I tried is generating a popup when someone uses a string in stead of numeric characters. But till now I can't get it working. The code i've tried is:
    :
    : If Not TextBox1.Text = Nothing Then
    :
    : If Asc(TextBox1.Text) < 47 Then
    : If Asc(TextBox1.Text) > 58 Then
    : MessageBox.Show("Only numeric input is allowed", , MessageBoxButtons.OK, MessageBoxIcon.Information)
    : End If
    : End If
    : End If
    :
    : Thanks in advance,
    :
    : Ernesto
    :

    Try this.

    [code]
    If Not TextBox1.Text = vbNullString Then
    If Not IsNumeric(TextBox1.Text) Then
    MessageBox "Only numeric input is allowed", vbOKOnly + vbInformation
    End If
    End If
    [/code]

    Regards,

    ______
    [size=2][b][blue]J[/blue]omac.[/b][/size]

  • : : I'm writing a small application. The only input needed are some numeric values. Can anyone tell me which code I have to write to manage that only numeric input is allowed in a text box.
    : : The solution which I tried is generating a popup when someone uses a string in stead of numeric characters. But till now I can't get it working. The code i've tried is:
    : :
    : : If Not TextBox1.Text = Nothing Then
    : :
    : : If Asc(TextBox1.Text) < 47 Then
    : : If Asc(TextBox1.Text) > 58 Then
    : : MessageBox.Show("Only numeric input is allowed", , MessageBoxButtons.OK, MessageBoxIcon.Information)
    : : End If
    : : End If
    : : End If
    : :
    : : Thanks in advance,
    : :
    : : Ernesto
    : :
    :
    : Try this.
    :
    : [code]
    : If Not TextBox1.Text = vbNullString Then
    : If Not IsNumeric(TextBox1.Text) Then
    : MessageBox "Only numeric input is allowed", vbOKOnly + vbInformation
    : End If
    : End If
    : [/code]
    :
    : Regards,
    :
    : ______
    : [size=2][b][blue]J[/blue]omac.[/b][/size]
    :
    :
    Thanks a lot for your reaction. It works great. Sometimes life is very simple.

    Greetings Ernesto
  • Not a problem, and thanks for taking the time to say thanks, so many don't.

    Regards and happy programming,
    ______
    [size=2][b][blue]J[/blue]omac.[/b][/size]

  • If you put the code in the event txtTextbox_Change() then it will check for the numeric only during input. With a little change to the other code you can make it faster!

    That would require only to look at the last entered value. Which can be found by using mid and len.

    [CODE]
    Private Sub txtTextbox_Change()
    If IsNumeric(mid(txtTextbox,Len(txtTextbox),1)) = True then
    Exit Sub
    Else
    MsgBox "Hey!! Numeric value only!!"
    End If
    End Sub
    [/CODE]

    [HR]
    [CODE]
    If the frontdoor is closed then why don't you try the backdoor?
    [/CODE]
    [PURPLE]Greetz from DarQ[/PURPLE]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories