Results 1 to 9 of 9

Thread: Check if numeric

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 1999
    Location
    Oak Creek, WI, USA
    Posts
    92

    Check if numeric

    How can I check if a value entered into a text box is numeric or not? Thanks.

  2. #2

  3. #3
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Just an example...

    IsNumeric Function Example:

    This example uses the IsNumeric function to determine if a variable can be evaluated as a number.
    VB Code:
    1. Dim MyVar, MyCheck
    2. MyVar = "53"   ' Assign value.
    3. MyCheck = IsNumeric(MyVar)   ' Returns True.
    4.  
    5. MyVar = "459.95"   ' Assign value.
    6. MyCheck = IsNumeric(MyVar)   ' Returns True.
    7.  
    8. MyVar = "45 Help"   ' Assign value.
    9. MyCheck = IsNumeric(MyVar)   ' Returns False.
    Help keep this forum clean: Remember to mark your thread as resolved · Search before you post · Remember to rate posts that help

    VS2010: Visual Studio 2010 Keybinding Posters
    · Service Pack 1
    Tools: GhostDoc - automatically generates XML documentation comments
    · NuGet package Manager · PowerCommands IDE extensions
    Source Control: ankhsvn - integration for SVN
    · Windows Shell Extension for Subversion

    Development Laptop: Intel Core i5 430M 2.26 GHz @ 2.53 GHz
    · 4096 MB, DDR3 PC3-8500F (533 MHz), Kingston · ATI Mobility Radeon HD 5470 · 15.6 @ 16:9, 1366x768 pixel, HD LED LCD

    I follow:
    JoelOnSoftware - A weblog by Joel Spolsky, a programmer working in New York City, about software and software companies
    ScottGu's Blog - Scott Guthrie works for Microsoft as the Product Manager of the .NET Framework
    Portugal-a-Programar - Portuguese Developers Community
    .NET Rocks! - is a weekly Internet audio talk show for .NET Developers.

    Programming Languages:
    C#
    · VB.NET · JAVA · PHP · Javascript
    Other:
    XML
    · HTML · CSS · JQuery · SQL



    *** Proudly Portuguese ***

  4. #4
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    The Manual Way

    This stuff should work for integers -- off the top of my head (haven't tried it out though):

    VB Code:
    1. Private Function IsNumeric(ByVal argData As String) As Boolean
    2.  
    3.   Dim chrCtr As Long    ' loop counter
    4.   Dim curChr As String  ' current character
    5.  
    6.   If Len(argData) = 0 Then Exit Function
    7.  
    8.   ' Run through the passed-in argument.
    9.  
    10.   For chrCtr = 1 To Len(argData)
    11.     Let curChr = Mid$(chrCtr, argData, 1)                  ' get a character
    12.     If curChr < "0" Or curChr > "9" Then Exit Function ' << BOOMER! not numeric
    13.   Next chrCtr
    14.  
    15.   Let IsNumeric = True  ' set retval (return value)
    16.  
    17. End Function

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    BTW, I've developed an OCX which is basically a textbox that restricts input to numeric values. It doesn't allow the use of a mask but in addition to the numeric-only restriction and the normal textbox properties it has three new properties which I hope are self-explanatory:
    CanBeNegative (Boolean)
    CanHaveDecimals (Boolean)
    MaxDecimals (Integer)

    If anyone is interested I'll attach the OCX.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I'm interested.

    (Source code included??)

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Sure, why not. Make sure you store the OCX in your Windows System folder. Use the OCX and the code at your own risk. I just wrote it a couple of days ago and haven't spent a lot of time testing it although it seems to work correctly. If you find any bugs or if you have suggestions for improvement, please let me know.
    Attached Files Attached Files

  8. #8
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    Originally posted by MartinLiss
    ...I just wrote it ... and haven't spent a lot of time testing it... If you find any bugs ... please let me know.
    I found a bug, but it should be simple to work around.

    I downloaded the OCX and tried it out. The code seems pretty much OK for English locales ONLY. The decimal point is a comma in most European countries. Anyway, that is not a bug per se ... it is more like a lack of internationalization. The real bug is here:

    1) In the number box, type "-153.45"
    2) Press <Shift>+<Home>
    3) Press <Shift>+<Delete>
    4) Press <Shift>+<Insert>
    5) Repeat step 4

    After these 5 steps, your box will contain "-153.45-153.45".

    You need to handle the <Insert> key either by disabling it, but will it then be possible to cause this error using the context menu? I think it would be better to move some of your code from the KeyPress event to the Change event.

    Just a suggestion.

  9. #9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width