473,687 Members | 3,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.IsNum ber(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 3406
REM txtAmountFigure = control.
Dim intLoc As Integer = txtAmountFigure .Text.LastIndex Of("."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****@discuss ions.microsoft. com> schreef in bericht
news:5C******** *************** ***********@mic rosoft.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.IsNum ber(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****@discuss ions.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.SelectionSta rt <> 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.
"Crouchie19 98" 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.SelectionSta rt <> 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.LastIndex Of("."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****@discuss ions.microsoft. com> schreef in bericht
news:5C******** *************** ***********@mic rosoft.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.IsNum ber(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****@discuss ions.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****@discuss ions.microsoft. com> a écrit dans le message de news:
5C************* *************** **...icrosof 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.IsNum ber(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
"Crouchie19 98" <cr**********@d iscussions.micr osoft.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****@discuss ions.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13703
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 18:39:24 +0200 Local: Fri,Jul 15 2005 12:39 pm Subject: Re: Summer 2005 browsers' test set ?
5
21476
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 C#.NET. I may be just having a paradigm shift issue and I just don't understand new the methods fully, yet. I would love to use the Validation methods, but they occur WAY TOO LATE in the event order list! I don't want the TextBox.Text to ever have...
16
8361
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 perfectly. Some do only allow numeric - but you cannot backspace. Maybe that is "good enough" - but surely it is not that difficult to have a textbox that only accepts numeric values and backspaces.
0
1618
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 into the text box... Option Strict On Option Explicit On Imports System Imports System.Windows.Forms Imports System.ComponentModel
1
1911
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, VB.Net automatically raise error on lost focus of textbox, but I don't want user to type anything else except Numeric, any property to be set in Textbox?
4
6945
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) can anyone provide a snippet of code please many thanks
23
138304
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
2785
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 should happen while entering it self . I am looking for the piece of code in VB.NET. Please do help me Thanks
5
28596
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 AlphaNumeric = 2 Numeric = 3 End Enum
0
8590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9066
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8783
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6450
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4541
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2960
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2214
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1947
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.