Newbie For Loop Help

AWinLeo
12-04-2008, 02:22 PM
Hi, I'm taking a VB class in school, and I'm going nuts trying to figure something out.

Basically, I need to make a password verification app. It has to be at least 6 characters long, and contain 1 number, and 1 letter. I've been able to get the number check, and length check, but I can't get the letter check.

I thought what the best idea would be is to make a for loop to check for numbers, and if the amount of numbers equals the length of the string, then it would fail the check.

What I coded was this:

For i = 0 To 9
For j = 0 To 9
If txtPass.Text.IndexOf(i) = j Then
intLetterCheck = intLetterCheck + 1
End If
Next j
Next i

If txtPass.Text.Length = intLetterCheck Then
MessageBox.Show("The password must contain a letter!", "No letter.", MessageBoxButtons.OK, MessageBoxIcon.Error)
intLetterVerify = 1
End If

The problem is, that intLetterCheck always just shows the value of 1.

Any advice as to what I'm missing?

AtmaWeapon
12-04-2008, 03:01 PM
You're really overcomplicating this, and also inadvertently only checking the first 9 characters of the string for a number. This would make a password like "EnglandFranceGermany3" fail, even though it's valid, since IndexOf(3) would return 20. Since 20 > 9, it will never be equal to j.

There's a simpler way. The shared methods Char.IsLetter and Char.IsDigit will tell you if a given character is a letter or a digit. Due to a clever implementation by the .NET designers, you can treat a string as if it were an array of characters. With these two facts, you should be well on your way. Here's a hint, with ??? in places where you fill in the blanks.:

Dim foundDigit As Boolean = False
Dim foundLetter As Boolean = False

For i As Integer = 0 To txtPass.Text.Length - 1
If Char.IsDigit(txtPass.Text(i)) Then
???
End If

If ???(txtPass.Text(i)) Then
foundLetter = ???
End If
Next

Dim passwordValid as Boolean = ???

AWinLeo
12-07-2008, 04:14 PM
Thank you very much for your help.

My apologies for the long reply. Finals have me slammed, and I had to finish some things before my VB final.

Cheers!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum