CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2001
    Posts
    3

    Compare Str to Rules

    I am working on a password comparison tool.
    My question revolves around comparing if specific rules were met with password.
    The rules are
    1 PSWD Str Length greater then 6 less then 20
    -Solved with Len() function
    2 PSWD must contain at least one number key and
    one special key i.e. !@#$%^&*()
    my question regards the second rule.

    How do I compare a Str to ensure it contains at least one number and one special key?
    any help would be appreciated
    Thanks



  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Compare Str to Rules

    Use this fast function to test for the occurrence of nonalphanumeric characters in a string:

    Private Declare Function StrSpn Lib "SHLWAPI" Alias _
    "StrSpnW" (ByVal psz As Long, ByVal pszSet As Long) As Long

    Public Function IsAlphaNum(ByVal sString As String) As Boolean
    Dim lPos As Long

    Const ALPHA_NUM As String = "abcdefgihjklmnopqrstuvwxyz" & _
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

    ' Returns the first occurrence of nonmatching characters
    lPos = StrSpn(StrPtr(pString), StrPtr(pAlphaNum))
    ' If the return position is not equal to the length of the
    ' input string, nonalphanumeric chars were found.
    IsAlphaNum = (lPos = Len(sString))
    End Function

    You can easily modify this function to scan for invalid characters by editing the ALPHA_NUM constant so it
    includes only characters you consider legal.


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Sep 2001
    Location
    Maine
    Posts
    15

    Re: Compare Str to Rules

    Another method would be to get the Len on the password and then merely go through each position on the string and compare it to your ASCII code values that are acceptable for the special keys and then another loop to check for the number key.

    Dim Length as Integer
    Length = len(string)
    for i = 1 to len(string)
    Asc(mid(string, i, 1)) = (Your ASCII Codes)
    next i





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured