By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,032 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,032 IT Pros & Developers. It's quick & easy.

Only Numeric Values in the textbox

P: n/a
I have written the following code which allows user to enter only numbers and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering if
some can modify the following code so that it be done in keypress event.

Thanks in advance!
If Asc(e.KeyChar) <> Keys.Back Then
If e.KeyChar.IsNumber(e.KeyChar) = False Then
If e.KeyChar <> "." Then
ze.Handled = True
Else
If txtAmountFigure.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If
End If
End If

Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
REM txtAmountFigure = control.
Dim intLoc As Integer = txtAmountFigure.Text.LastIndexOf("."c)
e.Handled = true
If intLoc >= 0 Then
If .Text.Substring(intLoc).Length <= 2 Then
e.Handled = False
End If
Else
e.Handled = False
End If

"patang" <pa****@discussions.microsoft.com> schreef in bericht
news:5C**********************************@microsof t.com...
I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.

Thanks in advance!
If Asc(e.KeyChar) <> Keys.Back Then
If e.KeyChar.IsNumber(e.KeyChar) = False Then
If e.KeyChar <> "." Then
ze.Handled = True
Else
If txtAmountFigure.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If
End If
End If

Nov 21 '05 #2

P: n/a
"patang" <pa****@discussions.microsoft.com> schrieb:
I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.

I don't think that this is a good idea. Validation should be made when the
control is validated ('Validating' event), not when the user enters text.
This allows the user to do in-place editing of content pasted from the
clipboard, for example. Typically an error provider is set if the text
entered into the textbox is invalid.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
Put this into the KeyPress event:

Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 48 To 57, 8, 13
Case 45
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
If Me.SelectionStart <> 0 Then
KeyAscii = 0
End If
Case 46
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If

This accepts a decimal point, a minus sign too.

If Herfried wants to be pedantic about the above code then I will post the
code to stop all copy/paste/cut shorcut handlers too because Herfried once
before mentioned it. Although, he never produced a solution.
Nov 21 '05 #4

P: n/a
Your code is not doing what I want.

I don't want user to enter more than 2 digits after the decimal place.
"Crouchie1998" wrote:
Put this into the KeyPress event:

Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 48 To 57, 8, 13
Case 45
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
If Me.SelectionStart <> 0 Then
KeyAscii = 0
End If
Case 46
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If

This accepts a decimal point, a minus sign too.

If Herfried wants to be pedantic about the above code then I will post the
code to stop all copy/paste/cut shorcut handlers too because Herfried once
before mentioned it. Although, he never produced a solution.

Nov 21 '05 #5

P: n/a
Your code doesn't work.

Because once user enters two digits AFTER the decimal place he CANNOT enter
anything even on the LEFT HAND SIDE of the decimal place. That's not good.
"Qwert" wrote:
REM txtAmountFigure = control.
Dim intLoc As Integer = txtAmountFigure.Text.LastIndexOf("."c)
e.Handled = true
If intLoc >= 0 Then
If .Text.Substring(intLoc).Length <= 2 Then
e.Handled = False
End If
Else
e.Handled = False
End If

"patang" <pa****@discussions.microsoft.com> schreef in bericht
news:5C**********************************@microsof t.com...
I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.

Thanks in advance!
If Asc(e.KeyChar) <> Keys.Back Then
If e.KeyChar.IsNumber(e.KeyChar) = False Then
If e.KeyChar <> "." Then
ze.Handled = True
Else
If txtAmountFigure.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If
End If
End If


Nov 21 '05 #6

P: n/a
Don't you think it would be better to PREVENT user TYPING alphabets in the
numeric-only field instead of letting him enter first then validating it
later on and prompting a message.

"Herfried K. Wagner [MVP]" wrote:
"patang" <pa****@discussions.microsoft.com> schrieb:
I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.

I don't think that this is a good idea. Validation should be made when the
control is validated ('Validating' event), not when the user enters text.
This allows the user to do in-place editing of content pasted from the
clipboard, for example. Typically an error provider is set if the text
entered into the textbox is invalid.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7

P: n/a
You may have a look at this site:
http://www.dotnetmasters.com/samples.htm
http://www.dotnetmasters.com/SampleC...rsVersion3.zip

Billy Hollis has implemented some nice validators (including numeric one)

José

"patang" <pa****@discussions.microsoft.com> a écrit dans le message de news:
5C**********************************@microsoft.com...
I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.

Thanks in advance!
If Asc(e.KeyChar) <> Keys.Back Then
If e.KeyChar.IsNumber(e.KeyChar) = False Then
If e.KeyChar <> "." Then
ze.Handled = True
Else
If txtAmountFigure.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If
End If
End If

Nov 21 '05 #8

P: n/a
"Crouchie1998" <cr**********@discussions.microsoft.com> schrieb:
If Herfried wants to be pedantic about the above code then I will post the
code to stop all copy/paste/cut shorcut handlers too because Herfried once
before mentioned it. Although, he never produced a solution.


Do you think it's more user-friendly to disallow the user to use the
clipboard?!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9

P: n/a
"patang" <pa****@discussions.microsoft.com> schrieb:
Don't you think it would be better to PREVENT user TYPING alphabets in the
numeric-only field instead of letting him enter first then validating it
later on and prompting a message.


I typically allow the user to enter numbers in hexadecimal format (at least
when the textbox accepts integer numbers). This allows the user to paste
output of another program from the clipboard without the need to manipulate
it. In addition to that, I allow the user to enter thousands separators
("1,000,000.000,000"). 'Integer.Parse'/'Single.Parse'/'Double.Parse' have
an overloaded version which allows to specify different formats.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10

P: n/a
Ok...how about this one, a TextChanged event:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged

Dim intLoc As Integer = Me.TextBox1.Text.LastIndexOf("."c)
Dim intPos As Integer = Me.TextBox1.SelectionStart()

If intLoc >= 0 Then
If Me.TextBox1.Text.Substring(intLoc).Length > 3 Then
Me.TextBox1.Text = Me.TextBox1.Text.Substring(0,
Me.TextBox1.Text.Length - 1)
Me.TextBox1.SelectionStart = intPos
End If
End If

End Sub

"patang" <pa****@discussions.microsoft.com> schreef in bericht
news:64**********************************@microsof t.com...
Your code doesn't work.

Because once user enters two digits AFTER the decimal place he CANNOT
enter
anything even on the LEFT HAND SIDE of the decimal place. That's not good.
"Qwert" wrote:
REM txtAmountFigure = control.
Dim intLoc As Integer = txtAmountFigure.Text.LastIndexOf("."c)
e.Handled = true
If intLoc >= 0 Then
If .Text.Substring(intLoc).Length <= 2 Then
e.Handled = False
End If
Else
e.Handled = False
End If

"patang" <pa****@discussions.microsoft.com> schreef in bericht
news:5C**********************************@microsof t.com...
>I have written the following code which allows user to enter only
>numbers
>and
> only one decimal point. This works fine.
>
> However, I want that user shouldn't enter a third number after decimal
> point, for example user should not be allowed to enter 12557.234. User
> should
> only be allowed to enter two digits after decimal point.
>
> I know I can validate it in the validate event, however, I was
> wondering
> if
> some can modify the following code so that it be done in keypress
> event.
>
> Thanks in advance!
>
>
> If Asc(e.KeyChar) <> Keys.Back Then
> If e.KeyChar.IsNumber(e.KeyChar) = False Then
> If e.KeyChar <> "." Then
> ze.Handled = True
> Else
> If txtAmountFigure.Text.IndexOf(".") <> -1 Then
> e.Handled = True
> End If
> End If
> End If
> End If
>


Nov 21 '05 #11

P: n/a
Sorry this is better:

Dim intPos As Integer = Me.TextBox1.SelectionStart()
Dim intLoc As Integer = .Text.LastIndexOf("."c)
If intLoc >= 0 Then
If .Text.Substring(intLoc).Length <= 2 Or intPos <= intLoc Then
e.Handled = False
End If
Else
e.Handled = False
End If

"patang" <pa****@discussions.microsoft.com> schreef in bericht
news:64**********************************@microsof t.com...
Your code doesn't work.

Because once user enters two digits AFTER the decimal place he CANNOT
enter
anything even on the LEFT HAND SIDE of the decimal place. That's not good.
"Qwert" wrote:
REM txtAmountFigure = control.
Dim intLoc As Integer = txtAmountFigure.Text.LastIndexOf("."c)
e.Handled = true
If intLoc >= 0 Then
If .Text.Substring(intLoc).Length <= 2 Then
e.Handled = False
End If
Else
e.Handled = False
End If

"patang" <pa****@discussions.microsoft.com> schreef in bericht
news:5C**********************************@microsof t.com...
>I have written the following code which allows user to enter only
>numbers
>and
> only one decimal point. This works fine.
>
> However, I want that user shouldn't enter a third number after decimal
> point, for example user should not be allowed to enter 12557.234. User
> should
> only be allowed to enter two digits after decimal point.
>
> I know I can validate it in the validate event, however, I was
> wondering
> if
> some can modify the following code so that it be done in keypress
> event.
>
> Thanks in advance!
>
>
> If Asc(e.KeyChar) <> Keys.Back Then
> If e.KeyChar.IsNumber(e.KeyChar) = False Then
> If e.KeyChar <> "." Then
> ze.Handled = True
> Else
> If txtAmountFigure.Text.IndexOf(".") <> -1 Then
> e.Handled = True
> End If
> End If
> End If
> End If
>


Nov 21 '05 #12

P: n/a
Might I suggest that if you really want to do this as the user types you
abandon the attempt at blocking key strokes and do the following:

1) grab the string as the person types
2) run the string through a regex parser to see if it matches the pattern
3) if it matches the pattern allow the change. If it does not match the
pattern revert back to the last known good string.

I have not tested this out but it sounds like the start of a sound real time
validation scheme to me.

"patang" <pa****@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.

Thanks in advance!
If Asc(e.KeyChar) <> Keys.Back Then
If e.KeyChar.IsNumber(e.KeyChar) = False Then
If e.KeyChar <> "." Then
ze.Handled = True
Else
If txtAmountFigure.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If
End If
End If

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.