473,382 Members | 1,526 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,382 software developers and data experts.

Validating a Textbox to numeric

Hello. I have a text box but I only want users to be able to enter numbers and not text. Could anyone help me with the code. Thanks
Nov 20 '07 #1
9 1694
jamesd0142
469 256MB
Expand|Select|Wrap|Line Numbers
  1. Function IsNumeric(ByVal MyVal)
  2.         Dim i As Integer
  3.         Dim a As String
  4.         Dim MyValLen As String
  5.         MyValLen = Len(MyVal)
  6.         If MyValLen > 0 Then
  7.             For i = 1 To MyValLen
  8.                 a = Mid(MyVal, i, 1)
  9.                 If a <> "0" Then 'Or a <> "1" Or a <> "2" Then
  10.                     If a <> "1" Then
  11.                         If a <> "2" Then
  12.                             If a <> "3" Then
  13.                                 If a <> "4" Then
  14.                                     If a <> "5" Then
  15.                                         If a <> "6" Then
  16.                                             If a <> "7" Then
  17.                                                 If a <> "8" Then
  18.                                                     If a <> "9" Then
  19.                                                         Return False
  20.                                                         Exit Function
  21.                                                     End If
  22.                                                 End If
  23.                                             End If
  24.                                         End If
  25.                                     End If
  26.                                 End If
  27.                             End If
  28.                         End If
  29.                     End If
  30.                 End If
  31.             Next
  32.             Return True
  33.         End If
  34.     End Function
  35.  
use this function like:
Expand|Select|Wrap|Line Numbers
  1. if isnumeric(textbox2.text) = true then
  2. 'code here
  3. end if
  4.  
Nov 20 '07 #2
kadghar
1,295 Expert 1GB
use this function like:
Expand|Select|Wrap|Line Numbers
  1. if isnumeric(textbox2.text) = true then
  2. 'code here
  3. end if
  4.  
another way could be using the ascii code, something like:

Expand|Select|Wrap|Line Numbers
  1. public function isnumeric(byval str1 as string) as boolean
  2. isnumeric = true
  3. i=1
  4. do
  5.     j= asc(str1,i,1)
  6.     if (j < 48 or j > 59) and j <> 46 then
  7.         isnumeric = false
  8.         exit do
  9.     end if
  10.     i = i + 1
  11. loop until i > len(str1)
  12. end function
number 46 is for a dot, so you'll also acept decimals

Im not that familiarized with vbnet, but in other versions of vb IsNumeric is an existing function, you dont have to create it.
Nov 20 '07 #3
lotus18
866 512MB
Check this:

http://www.thescripts.com/forum/thread736290.html

Use this function at keypress event of the textbox.
Nov 21 '07 #4
Killer42
8,435 Expert 8TB
You might also consider using the Masked Edit control rather than a textbox. The masked edit control gives you more control over the input.
Nov 21 '07 #5
Mohan Krishna
115 100+
Check this:

http://www.thescripts.com/forum/thread736290.html

Use this function at keypress event of the textbox.
Hi

It is very good! And I wrote it several times in many projects!

ALL THE BEST!
Nov 21 '07 #6
lotus18
866 512MB
Hi

It is very good! And I wrote it several times in many projects!

ALL THE BEST!
Thanks for the acknowledgment : )
Nov 21 '07 #7
hi,
use the Keypress or keydown event

check for the ascii value range 48-57, if yes then make
keycode=0
or keypress=false

hope this will help you,

With regards
Venkatramasamy SN
Nov 23 '07 #8
dontbe
3
You use this to only number can accept by a Textbox
1. Declare this API at You Declration Section (in your form/module):
Expand|Select|Wrap|Line Numbers
  1. '&& Only Allowing Number In Texttbox
  2. Public Declare Function GetWindowLong Lib "user32"  Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
  3.  
  4. Public Declare Function SetWindowLong Lib "user32" Alias    "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  5.  
  6.  
2. Create Private or Public sun in fom/module, use this code:
Expand|Select|Wrap|Line Numbers
  1. Public Sub SetNumber(NumberText As TextBox, Flag As Boolean)
  2. Dim curstyle As Long
  3. Dim newstyle As Long
  4.  
  5.     curstyle = GetWindowLong(NumberText.hWnd, GWL_STYLE)
  6.  
  7.     If Flag = True Then
  8.        curstyle = curstyle Or ES_NUMBER
  9.     Else
  10.        curstyle = curstyle And (Not ES_NUMBER)
  11.     End If
  12.  
  13.     newstyle = SetWindowLong(NumberText.hWnd, GWL_STYLE, curstyle)
  14.     NumberText.Refresh
  15. End Sub
  16.  
3. And then you call that sun in any event, as example:
Expand|Select|Wrap|Line Numbers
  1. Public Sub Form_Load()
  2.        SetNumber textBox1, True
  3. End Sub
  4.  
Hope this work for you, i think it more simple than using a lot of loops..
Nov 23 '07 #9
lotus18
866 512MB

Hope this work for you, i think it more simple than using a lot of loops..
You created an API function where in fact you can do this with more simple code
Nov 23 '07 #10

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

Similar topics

1
by: Mike | last post by:
Hi, In one of my winforms I am validating a textbox making sure it was filled out and that the data is numeric and setting e.Cancel = true if not. Everything works fine however the form has a OK...
6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
4
by: Eric | last post by:
Is there a way to cancel the validating event on the closing event? I have 2 textboxes that I use the validating event to check for numeric data. If I try to close the form without putting a...
2
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions...
6
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
8
by: Peted | last post by:
I have an amazing problem which i think i have no hope of solving Im working with a c# dot net module that is hosted by and runs under a delphi form envrioment. Dont ask me how this insanity has...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.