rnm89 Posted April 3, 2003 Posted April 3, 2003 I am trying to make sure a textbox that will response to numbers ONLY and limited to 10 digits. This is what I have, It allows for 10 numbers or letters before the button is enabled: 'make sure textbox4.text is a 10 digit number Dim length As Integer Dim string2 As String string2 = TextBox4.Text length = string2.Length If length < 10 Then Button2.Enabled = False If length > 10 Then Button2.Enabled = False If length = 10 Then Button2.Enabled = True Thanks for any help Quote
*Experts* Volte Posted April 3, 2003 *Experts* Posted April 3, 2003 Use a function like this to test if it's a number: Private Function IsNumber(ByVal str As String) As Boolean Try Dim temp As Integer = Integer.Parse(str) Catch ex As Exception Return False End Try Return True End FunctionAlso, you can set the MaxLength property for the TextBox to 10. Quote
Moderators Robby Posted April 3, 2003 Moderators Posted April 3, 2003 Just expand on Volte's solution, add this line after his Dim temp line... if temp.length = 10 then return true else return false end if 'then in the Validating event of the text box do this... if IsNumber(textbox4.text.tostring) then Button2.Enabled = True else Button2.Enabled = false end if Quote Visit...Bassic Software
hog Posted April 4, 2003 Posted April 4, 2003 isn't it simpler to do: IF IsNumeric(textbox4.Text) ANDALSO textbox4.length = 10 THEN Button2.Enabled=True ELSE Button2.Enabled=False Endif Quote My website
*Experts* Volte Posted April 4, 2003 *Experts* Posted April 4, 2003 Hmmm. Well, IsNumeric turned out to be in .NET after all; trouble is, I don't know if it's a "real" .NET function, or one of those backwards compatable VB6 ones. It's in 'Microsoft.VisualBasic.Information', so it seems like it would be a VB6 back-compatable function... :-\ In any case, hog's method will work. Quote
*Gurus* divil Posted April 4, 2003 *Gurus* Posted April 4, 2003 IsNumeric is part of the VB runtime library and does exactly the same as Volte's function above. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted April 4, 2003 *Experts* Posted April 4, 2003 Also, the Parse method allows you to specify what a valid number looks like, such as whether you want to allow a thousands separator, a decimal point, and more. There are some built-in types such as "System.Globalization.NumberStyles.Number" and "System.Globalization.NumberStyles.Integer". Here's a revised version of Volte's function that specifies what a valid number will look like: Private Function IsNumber(ByVal str As String) As Boolean Try ' I hope my _ syntax is still valid in VB.NET... Integer.Parse(str, _ System.Globalization.NumberStyles.AllowLeadingSign Or _ System.Globalization.NumberStyles.AllowLeadingWhite Or _ System.Globalization.NumberStyles.AllowThousands Or _ System.Globalization.NumberStyles.AllowTrailingWhite) Catch ex As Exception Return False End Try Return True End Function I also got rid of the dummy int variable. It wasn't being used and isn't necessary to get the Catch to work. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
wyrd Posted April 4, 2003 Posted April 4, 2003 :eek: If we're starting to go through all this trouble just to make an isdigit method, then why not just go ahead and use regular expressions to solve his problem .. which can be accomplished in 2-3 lines of code (not just the digit check, but also including the 10 letters check) :confused: Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Nerseus Posted April 4, 2003 *Experts* Posted April 4, 2003 Regular expressions are great (didn't Robby or Orbity put together a sample for them?). You can also set a TextBox's MaxLength property to limit them to 10 characters (hard-and-fast limit - not an "after the fact" type of limiter). I was mentioning the Parse method's overloads because they're useful elsewhere (such as for a Format/Parse event on a Bound control). A simple regular expression such as "^\d{0,10}$" should do the trick. It won't allow the user to type in "10,000" though - it will evaulate false. You can always tweak the expression to allow commas, a decimal point and more, but there are built in NumberStyles for those already (but you can't use them with regular expressions). Ah well, I wonder if there are more ways to validate numeric input than there are number of licks to get to the center of a tootsie roll tootsie pop... -nerse Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
wyrd Posted April 4, 2003 Posted April 4, 2003 Ah, I see... I read his problem a little differently. To me it sounded like he wanted a text field that accepted some sort of key of a specific size (ie; 5312355792 or lekwnsjghz) Quote Gamer extraordinaire. Programmer wannabe.
Moderators Robby Posted April 4, 2003 Moderators Posted April 4, 2003 Nice function Nerseus. (the first one) Quote Visit...Bassic Software
rnm89 Posted April 4, 2003 Author Posted April 4, 2003 Thanks for all of the input. As wyrd described it, I am trying to limit the amount of digits & ensure that all of the inputs are numbers for validating and so there is NO mistake of reversing a zero for the letter OH Simple mistakes can screw everything up!!!! I will give the expamles a try tonight and let you know. Once again thanks for everyones help. :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.