How do I make sure that there are 10 numbers in a txtbox?

Re: txtBox help by Justin

Justin
Sat Nov 06 20:57:56 CST 2004

10 numbers or 10 digits?

private bool Validate10Digits(TextBox text) {
if ( text.Length != 10 ) { return false; } // not 10 of them for sure
for(int i = 0; i < text.Length; i++) {
char trans = text[i] - '0';
if ( trans < 0 || trans > 9 ) { return false; }
}

return true;
}

10 numbers is a variation of the above.

private bool Validate10Numbers(TextBox text) {
if ( text.Length < 19 ) { return false; } // basic check
string[] numbers = text.Split(',');
if ( numbers.Length != 10 ) { return false; }
for(int i = 0; i < numbers.Length; i++) {
if ( !ValidateNumber(i) ) { return false;
}

return true;
}

ValidateNumber is a variation of Validate10Digits that takes a string instead
of a TextBox and then removes the initial check.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Dave" <Dave@discussions.microsoft.com> wrote in message
news:8C071206-9D5D-42E6-8429-4C3C0BD686EE@microsoft.com...
> How do I make sure that there are 10 numbers in a txtbox?



Re: txtBox help by Dave

Dave
Sat Nov 06 22:46:06 CST 2004

How do I code in VB?

"Justin Rogers" wrote:

> 10 numbers or 10 digits?
>
> private bool Validate10Digits(TextBox text) {
> if ( text.Length != 10 ) { return false; } // not 10 of them for sure
> for(int i = 0; i < text.Length; i++) {
> char trans = text[i] - '0';
> if ( trans < 0 || trans > 9 ) { return false; }
> }
>
> return true;
> }
>
> 10 numbers is a variation of the above.
>
> private bool Validate10Numbers(TextBox text) {
> if ( text.Length < 19 ) { return false; } // basic check
> string[] numbers = text.Split(',');
> if ( numbers.Length != 10 ) { return false; }
> for(int i = 0; i < numbers.Length; i++) {
> if ( !ValidateNumber(i) ) { return false;
> }
>
> return true;
> }
>
> ValidateNumber is a variation of Validate10Digits that takes a string instead
> of a TextBox and then removes the initial check.
>
>
> --
> Justin Rogers
> DigiTec Web Consultants, LLC.
> Blog: http://weblogs.asp.net/justin_rogers
>
> "Dave" <Dave@discussions.microsoft.com> wrote in message
> news:8C071206-9D5D-42E6-8429-4C3C0BD686EE@microsoft.com...
> > How do I make sure that there are 10 numbers in a txtbox?
>
>
>

Re: txtBox help by Herfried

Herfried
Sun Nov 07 07:45:00 CST 2004

"Dave" <Dave@discussions.microsoft.com> schrieb:
> How do I make sure that there are 10 numbers in a
> txtbox?

Written from scratch:

\\\
Private Function ValidateInput(ByVal Text As String) As Boolean
If Text.Lengh <> 10 Then
Return False
End If
For Each c As Char In Text
If Not Char.IsDigit(c) Then
Return False
End If
Next c
Return True
End Function
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>