473,704 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

numeric validation

1 New Member
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


can anyone provide a snippet of code please

many thanks
May 17 '07 #1
3 2214
Frinavale
9,735 Recognized Expert Moderator Expert
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


can anyone provide a snippet of code please

many thanks
Is this a desktop application or web application.
If it's a web application it depends on what you're trying to do...do you want to just inform the user that the value they entered was not a number?

Or do you want to prevent them from even entering numbers?
This method requires a Client side JavaScript that checks for you...but you'll still have to check Server side (in your code behind) to make sure it is a number. Otherwise you could run into some problems.

-Frinny
May 17 '07 #2
Abdul Haque
17 New Member
its very simply , just use a client side script that mask over the key press
have a look

function maskNumeric(evt )
{
var charCode = (evt.which) ? evt.which : event.keyCode
if ((charCode < 48 || charCode > 57) )
return false;
return true;
}


no call this function on click event
like
onKeyPress="ret urn validTaskCode(e vent)"
May 23 '07 #3
Frinavale
9,735 Recognized Expert Moderator Expert
its very simply , just use a client side script that mask over the key press
have a look
Expand|Select|Wrap|Line Numbers
  1. function maskNumeric(evt)
  2. {        
  3. var charCode = (evt.which) ? evt.which : event.keyCode
  4. if ((charCode < 48 || charCode > 57) )
  5.     return false;
  6. return true;
  7. }
  8.  
no call this function on click event
like
onKeyPress="ret urn validTaskCode(e vent)"
Thanks!
Abdul Haque has given a great example on how to JavaScript to prevent people from entering anything other than a number.

I strongly suggest that you still make sure that this is a number when the form is submitted to the server. People can get around JavaScript and submit invalid.

Just do a "Try, Catch" around an Integer.Parse(t extBox.Text)... if it throws an exception then they've not provided a number.

-Frinny
May 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

30
3682
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code seems unnecessarily long. For some while, the following approach has been given here :- function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
16
8393
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.
2
2915
by: Chad | last post by:
I have a textbox control, txtMeasurement, that I want to allow only numeric decimal input. I thought to use a client side validation control to ensure that the data entered is of type "Double". 'Measurement Validation (Range Validator) Dim MeasurementRangeValidator As New RangeValidator 'validate that the measure is numeric MeasurementRangeValidator.ControlToValidate = txtMeasurement.ID| MeasurementRangeValidator.Display =...
2
9106
by: craig.wagner | last post by:
I have an element in my schema defined as follows: <xs:element name="BillingDate" type="xs:dateTime" nillable="true" minOccurs="0"/> I use the schema to validate incoming documents using an XmlValidatingReader in .NET 1.1. If the document contains the above element with no data, for example:
1
1867
by: dineshsanduke | last post by:
hi i m new user i created programatic data grid. i want numeric cell validation in data grid view
8
18183
by: .Net Sports | last post by:
I am checking for text input on a form validation in javascript that required at least one numeric character along with any number of alpha characters for a given input text box. The below is a var declare that does a method to check for alpha, or numeric, or - _ characters var charpos = objValue.value.search(""); but doing: var charpos = objValue.value.search("");
4
21512
by: Thiyagusathya | last post by:
Hi., Please help me for java script validation for text and numeric field.
3
2082
by: aris1234 | last post by:
Hi.. i have field in record using type : numeric, how to make validation and i want viewing messagebox if user inputing character or empty? this code just work for empty field, not for numeric type, if(WithoutContent(document.add.sprice.value)) { errormessage += "\n\n\"PRICE\" still Empty."; } ----------
16
12200
by: tshad | last post by:
I have a string field and a decimal fields and was wondering what the best way to check for numeric is? I have to do something like: If (myAmount is numeric) { total += myAmount; }
0
8684
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9271
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
9134
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...
1
9025
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8975
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...
0
7887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6604
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
4699
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2078
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.