Discussion:
Determine if a new password is alphanumeric
(too old to reply)
bj
2006-09-26 22:38:50 UTC
Permalink
What is the best way to determine in coldfusion if a user has created an
alphanumeric password?
Abinidi
2006-09-26 23:22:40 UTC
Permalink
<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") >
bj
2006-09-27 03:45:24 UTC
Permalink
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
Post by Abinidi
<cfif REFind("[^a-zA-Z0-9]", passwordstring)>
-- code to do whatever with password
<cfelse>
code to reject users password
</cfif>
<cfif REFind("[[:alnum:]]", "passwordstring") >
Adam Cameron
2006-09-27 09:44:32 UTC
Permalink
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
bj
2006-09-27 22:35:27 UTC
Permalink
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.
Post by Adam Cameron
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
Dan Bracuk
2006-09-27 15:29:34 UTC
Permalink
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
Sabaidee
2006-09-27 23:27:19 UTC
Permalink
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...
healey_mark
2006-10-03 23:40:12 UTC
Permalink
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.




<html>
<head><title>test pass</title></head>
<cfset
regex="^([a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*[0-9][a-zA-Z0-9]*|[a-zA-Z0-9]*[0-9][a-z
A-Z0-9]*[a-zA-Z][a-zA-Z0-9]*)$">
<body>
abc123:
<cfif REFind(regex, "abc123")>
yes
<cfelse>
no
</cfif>
<br>123
<cfif REFind(regex, "123")>
yes
<cfelse>
no
</cfif>
<br>abc
<cfif REFind(regex, "abc")>
yes
<cfelse>
no
</cfif>
<br>123abc
<cfif REFind(regex, "123abc")>
yes
<cfelse>
no
</cfif>
<br>123abc!
<cfif REFind(regex, "123abc!")>
yes
<cfelse>
no
</cfif>
</body>
</html>
Ian Skinner
2006-10-04 14:26:19 UTC
Permalink
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 :-)

Continue reading on narkive:
Loading...