Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Newbie For Loop Help


Reply
 
Thread Tools Display Modes
  #1  
Old 12-04-2008, 02:22 PM
AWinLeo AWinLeo is offline
Newcomer
 
Join Date: Dec 2008
Posts: 2
Default Newbie For Loop Help


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?
Reply With Quote
  #2  
Old 12-04-2008, 03:01 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

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.:

Code:
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 = ???
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 12-07-2008, 04:14 PM
AWinLeo AWinLeo is offline
Newcomer
 
Join Date: Dec 2008
Posts: 2
Default

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!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->