473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Only Numeric Values in the textbox

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
12 3389
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
"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
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
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
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
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
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
"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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: anonieko | last post by:
This applies to javascript dynamic textbox onkey > > > Newsgroups: comp.lang.javascript From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by this author Date: Fri, 15 Jul 2005...
5
by: Brian Robbins | last post by:
In standard C/C++ SDK, MFC, or VCL if I wanted to make a TextBox (CEditBox, TEditBox, etc.) only except numbers I have dozens of ways to handle it. But none of those methods are working in...
16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
0
by: Tom Shelton | last post by:
This is a simple example of a numeric only text box. It allows only the entry of integer values. The advantage of this example is that it also handles user attempts to past non integer text data...
1
by: Luqman | last post by:
My textbox is bound to Typed Dataset, with a Numeric Field, I want the user should not type anything in textbox except Numeric, how can I ? The data type is : Sql Server Decimal(5,2) Currently,...
4
by: viv | last post by:
how do you trap keyboard codes for text box in VB2005 ? I'm looking for a code snippet to only allow numeric input in a text box (same as Key event in VB6, then use Keyascii to decide action) ...
23
by: mathewgk80 | last post by:
Hi all, I would like to get the Javascript code to check the textbox contains only numeric values or numbers.. I am using asp.net and c#.net.. Please help me.. Regards, Mathew
6
by: robintwos | last post by:
Hi I would like to validate the while entering the data in a textbox for numeric values. it means . i would like to throw an error message when the user enters a character value. validation...
5
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.