Results 1 to 12 of 12

Thread: [RESOLVED] hey how do I check if textbox has a leading zero

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Resolved [RESOLVED] hey how do I check if textbox has a leading zero

    Hey

    I have textboxes that must contain numbers, so I checked with ISNumeric. But I guess that a leading zero is allowed, even a zero on its own. How do I tackle this problem please?

    S
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  2. #2
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: hey how do I check if textbox has a leading zero

    Your question is not clear... You have not even indicated your .Net/VS version...

    If you are using .Net 3.5/2008 version then try the masked textbox. Alternatively, check .paul.'s signature, if I remember correctly, there was a link to such a thing. Alternatively, handle the KeyDown/KeyPress events (can't remember which one, only one will work, try KeyDown first) and if the key pressed is not a number then cancel the press by setting e.Handled = True.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: hey how do I check if textbox has a leading zero

    Leading zeroes and zeroes on their own will return True from IsNumeric so there's no problem. That said, I suggest that you call the TryParse method of the appropriate numeric type, e.g. Integer or Double, rather than IsNumeric. This is especially the case if you intend to use the value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: hey how do I check if textbox has a leading zero

    I think what the topicstarter wants is the disallow leading zeroes (or only zeroes). You can do this by checking for the actual text already in the textbox, aswell as the e.KeyChar, in the keypressed event.

  5. #5
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: hey how do I check if textbox has a leading zero

    Quote Originally Posted by NickThissen
    I think what the topicstarter wants is the disallow leading zeroes (or only zeroes). You can do this by checking for the actual text already in the textbox, aswell as the e.KeyChar, in the keypressed event.
    Oh... Well, that just enforces my point... Poorly formulated question will result in an inadequate answer...
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  6. #6
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: hey how do I check if textbox has a leading zero

    I agree with obi1, First of all please be more specific second what version of .NET/VS are you using?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: hey how do I check if textbox has a leading zero

    What I want to disallow the user to input a whole number with a leading zero or a zero on its own.

    any example of function please?
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  8. #8
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: hey how do I check if textbox has a leading zero

    You have been asked to state what version of .NET and VS you are using at least twice, please answer that.

    As for a function example, try something like this:
    Code:
    - First check if the textbox is empty
    - If yes: 
          - Check if the keychar is a zero
          - If yes:
                 - Disallow the keychar
          - End if
    - End if
    After this, you can use the standard 'IsNumeric' check you already seem to have (judging by your first post) to only allow numbers and backspace, delete etc.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: hey how do I check if textbox has a leading zero

    the version is 2005

    I have soemthing like this :

    Code:
        If IsNumeric(TextBox2.Text) AndAlso (TextBox2.Text.Substring(0, 1)) <> ZERO Then
    what can I do instead of ZERO?

    P.S
    Code:
    Public Function CheckNull_Numeric_NoLeading_Zeros(ByVal txtbox As TextBox) As Boolean
            If txtbox.Text <> "" AndAlso IsNumeric(txtbox.Text) AndAlso (txtbox.Text.Substring(0, 1)) <> "0" Then
                Return True 'check if empty, numeric and has no leading zero
            End If
        End Function
    This works , is there something more cool?
    Last edited by angelica; Aug 31st, 2008 at 02:03 PM.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  10. #10
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: hey how do I check if textbox has a leading zero

    Wow long function name... if i were you it may be better if you shortened the name.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: hey how do I check if textbox has a leading zero

    vb.net Code:
    1. If Not Integer.TryParse(myTextBox.Text, New Integer) OrElse myTextBox.Text.StartsWith("0") Then
    2.     'Not a valid value.
    3. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: hey how do I check if textbox has a leading zero

    OK, that 's fixes thankyou
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

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