|
-
Aug 31st, 2008, 07:28 AM
#1
Thread Starter
Frenzied Member
[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! 
-
Aug 31st, 2008, 07:33 AM
#2
Frenzied Member
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 
-
Aug 31st, 2008, 07:36 AM
#3
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.
-
Aug 31st, 2008, 09:27 AM
#4
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.
-
Aug 31st, 2008, 10:37 AM
#5
Frenzied Member
Re: hey how do I check if textbox has a leading zero
 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 
-
Aug 31st, 2008, 12:14 PM
#6
Frenzied Member
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?
-
Aug 31st, 2008, 12:19 PM
#7
Thread Starter
Frenzied Member
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! 
-
Aug 31st, 2008, 12:24 PM
#8
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.
-
Aug 31st, 2008, 12:38 PM
#9
Thread Starter
Frenzied Member
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! 
-
Aug 31st, 2008, 04:45 PM
#10
Frenzied Member
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.
-
Aug 31st, 2008, 06:21 PM
#11
Re: hey how do I check if textbox has a leading zero
vb.net Code:
If Not Integer.TryParse(myTextBox.Text, New Integer) OrElse myTextBox.Text.StartsWith("0") Then 'Not a valid value. End If
-
Sep 1st, 2008, 02:45 AM
#12
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|