how to 'make sure textbox4.text is a 10 digit number

rnm89

Newcomer
Joined
Apr 1, 2003
Messages
20
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
 
Use a function like this to test if it's a number:
Visual Basic:
    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 Function
Also, you can set the MaxLength property for the TextBox to 10.
 
Just expand on Volte's solution, add this line after his Dim temp line...
Visual Basic:
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
 
isn't it simpler to do:

IF IsNumeric(textbox4.Text) ANDALSO textbox4.length = 10 THEN

Button2.Enabled=True

ELSE

Button2.Enabled=False

Endif
 
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.
 
IsNumeric is part of the VB runtime library and does exactly the same as Volte's function above.
 
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:
Visual Basic:
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
 
: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:
 
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
 
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)
 
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.





:)
 
Back
Top