8 Replies Latest reply on Oct 4, 2006 7:26 AM by Newsgroup_User

    Determine if a new password is alphanumeric

    Level 7
      What is the best way to determine in coldfusion if a user has created an
      alphanumeric password?


        • 1. Determine if a new password is alphanumeric
          Abinidi Level 1
          <cfif REFind("[^a-zA-Z0-9]", passwordstring)>
          -- code to do whatever with password if it is alphanumeric
          <cfelse>
          code to do whatever with password if it is not all alphanumeric
          </cfif>

          You can also use:

          <cfif REFind("[[:alnum:]]", "passwordstring") >

          • 2. Re: Determine if a new password is alphanumeric
            Level 7
            thanks for the tip

            <cfif #REfind("[^a-zA-Z0-9]", password)#>
            <cfoutput>#password#</cfoutput>Password is alphanumeric
            <cfelse>
            <cfoutput>#password#</cfoutput>Password in not alphanumeric</cfif>

            I took your advice and I cant seem to get it working. Is my syntax wrong?
            Password is a form variable

            the above code always produces password is not alphanumic after
            entering 111aaa as password




            "Abinidi" <webforumsuser@macromedia.com> wrote in message
            news:efcco0$t8n$1@forums.macromedia.com...
            > <cfif REFind("[^a-zA-Z0-9]", passwordstring)>
            > -- code to do whatever with password
            > <cfelse>
            > code to reject users password
            > </cfif>
            >
            > You can also use:
            >
            > <cfif REFind("[[:alnum:]]", "passwordstring") >
            >
            >
            >


            • 3. Re: Determine if a new password is alphanumeric
              Level 7
              Can you clarify the requirement. Are you after passwords that are ONLY
              alphanumeric (ie: must only contain a-z and 0-9, but no other characters),
              or passwords that must have at least some alphanumeric characters in them
              (so all alpha is no good, nor is all numeric).

              The orginal regex matches any character that is not a-zA-Z0-9, so you have
              your logic around the wrong way. If it DOES return a match, the pwd has a
              non alpha-numeric character in it.

              --
              Adam
              • 4. Re: Determine if a new password is alphanumeric
                Dan Bracuk Level 5
                Regular expressions are not my strong suit, but, don't you have that backwards?

                If I do this.
                <cfset x = REFind("[^a-zA-Z0-9]", "passwordstring")>
                <cfdump var="#x#">
                I get 0

                If I do this:
                <cfset x = REFind("[^a-zA-Z0-9]", "pas*swordstring")>
                <cfdump var="#x#">
                I get 4
                • 5. Re: Determine if a new password is alphanumeric
                  Level 7
                  Hi Adam, thanks for the note. Im trying to make sure that new users register
                  to use my web site with an alphanumeric password.

                  That is, their password must contain letters and numbers.


                  "Adam Cameron" <adam_junk@hotmail.com> wrote in message
                  news:5e0tx0nksh9s.54x4xll3pziy.dlg@40tude.net...
                  > Can you clarify the requirement. Are you after passwords that are ONLY
                  > alphanumeric (ie: must only contain a-z and 0-9, but no other characters),
                  > or passwords that must have at least some alphanumeric characters in them
                  > (so all alpha is no good, nor is all numeric).
                  >
                  > The orginal regex matches any character that is not a-zA-Z0-9, so you have
                  > your logic around the wrong way. If it DOES return a match, the pwd has a
                  > non alpha-numeric character in it.
                  >
                  > --
                  > Adam


                  • 6. Re: Determine if a new password is alphanumeric
                    azadisaryev Level 1
                    i am afraid Abidini did get it the other way around and confused oyu with that.

                    Correct sequence should be:

                    <cfif REFind("[^a-zA-Z0-9]", passwordstring)><!--- NO alphanumeric characters in passwordstring --->
                    .....
                    <cfelse><!--- passwordstring contains an alphanumeric character --->
                    ....
                    </cfif>

                    if you are using <cfform> and <cfinput> tags in your form, then you can use CF validation on your password field:

                    <cfinput type="text" validate="regex" pattern="^[a-zA-Z0-9]*$" required="yes" message="your message to display if validation fails">

                    you must be using CFMX7 for this to work, though...
                    • 7. Re: Determine if a new password is alphanumeric
                      healey_mark Level 1
                      BJ,

                      The regular expressions given (like "^[a-zA-Z0-9]$" are just ensuring that the string contains only letters or numbers, but not forcing the inclusion of at least one of each.

                      My thought was the string must either be:
                      <stuff 1><one letter><stuff 2><one digit><stuff 3>
                      or
                      <stuff 1><one digit><stuff 2><one letter><stuff 3>
                      where stuff 1, stuff 2, and stuff 3 are arbitrary (possibly empty) alphanumeric strings.

                      The regex for this is:
                      "^([a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*[0-9][a-zA-Z0-9]*|[a-zA-Z0-9]*[0-9][a-zA-Z0-9]*[a-zA-Z] [a-zA-Z0-9]*)$"

                      The pipe "|" in the middle separates the two cases (the letter occurs before the number | the number occurs before the letter).

                      The ^ denotes the beginning of the string, and the $ denotes the end, so this RE will always return 1 if the password is OK, and zero otherwise.


                      • 8. Re: Determine if a new password is alphanumeric
                        Level 7
                        The regex for this is:

                        "^([a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*[0-9][a-zA-Z0-9]*|[a-zA-Z0-9]*[0-9][a-zA-Z0-9
                        ]*[a-zA-Z][a-zA-Z0-9]*)$"


                        I would probably have done this like this:

                        <cfif refind("[a-z]"...) AND refind("[A-Z]"...) AND refind("[0-9]"...)...>

                        I'm sure they both work :-)