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
Printable View
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
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.
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.
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...:wave:Quote:
Originally Posted by NickThissen
I agree with obi1, First of all please be more specific second what version of .NET/VS are you using?
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?
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:
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.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
the version is 2005
I have soemthing like this :
what can I do instead of ZERO?Code:If IsNumeric(TextBox2.Text) AndAlso (TextBox2.Text.Substring(0, 1)) <> ZERO Then
P.S
This works , is there something more cool?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
Wow long function name... if i were you it may be better if you shortened the name.
vb.net Code:
If Not Integer.TryParse(myTextBox.Text, New Integer) OrElse myTextBox.Text.StartsWith("0") Then 'Not a valid value. End If
OK, that 's fixes thankyou