finding multiple chars in password
I just interacted with an authentication system that one of it password rules is not to repeat a certain character 3 or more times. So it mad me think of how I would accomplish this validation if I wrote the code.
First thing that came to mind is a regular expressions (RE), i've tried and tried and couldn't figure out a RE that would solve any character repeated more than 3 times in a string. I guess I'm not an expert in RE! If anyone can figure this out, i'd appreciate knowing the RE.
Therefore, i moved my logic to just loop within a loop. Example below.
First thing that came to mind is a regular expressions (RE), i've tried and tried and couldn't figure out a RE that would solve any character repeated more than 3 times in a string. I guess I'm not an expert in RE! If anyone can figure this out, i'd appreciate knowing the RE.
Therefore, i moved my logic to just loop within a loop. Example below.
<cfloop from="1" to="#len(variables.tmpPassword)#" index="pos">
<cfset curChar = Mid(variables.tmpPassword,pos,1)>
<cfset curCharCount = 0>
<cfset curPos = FindNoCase(curChar,variables.tmpPassword)>
<cfloop condition="#curPos# GT 0">
<cfset curPos = FindNoCase(curChar,variables.tmpPassword,curPos+1)>
<cfset curCharCount = curCharCount + 1>
</cfloop>
<cfif curCharCount GTE 3>
<cfset eValidate.addValidationError("password","edu.psu.fps.cfc.fpsGTW.validateForPassword.4","The password you entered cannot contain three or more occurrences of the same character (#curChar#).")>
<cfbreak />
</cfif>
</cfloop>
<cfset curChar = Mid(variables.tmpPassword,pos,1)>
<cfset curCharCount = 0>
<cfset curPos = FindNoCase(curChar,variables.tmpPassword)>
<cfloop condition="#curPos# GT 0">
<cfset curPos = FindNoCase(curChar,variables.tmpPassword,curPos+1)>
<cfset curCharCount = curCharCount + 1>
</cfloop>
<cfif curCharCount GTE 3>
<cfset eValidate.addValidationError("password","edu.psu.fps.cfc.fpsGTW.validateForPassword.4","The password you entered cannot contain three or more occurrences of the same character (#curChar#).")>
<cfbreak />
</cfif>
</cfloop>
To find the same character three times in a row, do this:
ReplyDelete(.)\1\1
The ( ) part captures the contents of the group, the . is any single character (except newline), and the \1 refers to capture group 1.
So it captures any single character to group one, then match that character twice.
Wouldn't .{3,} be better? That would catch the same character repeated 3 or more times in a row.
ReplyDeleteNope, that says any character at least three times - it doesn't require the same character.
ReplyDelete(i.e the "{3,}" is applied before the "." is assigned, so it's equivalent to "...+" instead.)