CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2002
    Location
    England
    Posts
    17

    Question Validating text boxes?



    I have a text box, into which a user can type in a quantity.

    I need to validate this input to ensure that first, the text is not a letter, i.e is a number, and secondly that the number is a whole one, is an integer, not somethin like 2 or 4, but not a, or 3.5.


    Right, I have figured out how to make sure that the text in a text box in a number, IsNumeric, But I still can't find any code that validates wether the text is a decimal or not. I want to except only whole numbers.


    I need error reporting where the value in the txt box is a dud.

    Thanks for your help.
    Last edited by xeviva; December 3rd, 2002 at 05:04 PM.

  2. #2
    Join Date
    Dec 2002
    Posts
    4

    validate by checking for decimal

    You can validate by checking the text value of the box against characters in the string. For example:

    stringtocheck=textbox1.text
    if instr(stringtocheck,".")>0 then
    'what to do if decimal is found
    else
    'what to do if decimal is not found
    end if

    Hope this helps.

    psprogrammer

  3. #3
    Join Date
    Dec 2002
    Location
    England
    Posts
    17
    I tried something similar to this, but kept getting all entries into the textbox validated as if there was a decimal there?

  4. #4
    Join Date
    Dec 2002
    Posts
    4

    Use of instr function

    The instr looks for a literal character. If the decimal doesn't appear in the textbox the code the value will increase to the value of the position of the decimal in the string (if the number was 3.50 the value would be 2). The code provided should work for you.

  5. #5
    Join Date
    Oct 2002
    Location
    Växjö, Sweden
    Posts
    225
    Try checking the following...

    Code:
    If IsNumeric(TextBox1.Text) AndAlso TextBox1.Text / 1 = TextBox1.Text \ 1 Then
        'code
    End If
    /Leyan

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

Featured