Blog

Blog posts made on 21-Nov-07
21Nov
2007
ColdFusion UDF: RandString()

A user sent me some code he was having trouble with, and in the code I saw a rather verbose and complex block of CFML being used to generate random strings for use with CAPTCHA verifications. So, while replying to his e-mail, I also sent him the RandString() function I use myself. And, as it may be of use others, here it is:

view plain print about
1<!--- Generate random strings of specified length --->
2<cffunction name="RandString" output="no" returntype="string">
3    <cfargument name="length" type="numeric" required="yes">
4
5    <!--- Local vars --->
6    <cfset var result="">
7    <cfset var i=0>
8
9    <!--- Create string --->
10    <cfloop index="i" from="1" to="#ARGUMENTS.length#">
11        <!--- Random character in range A-Z --->
12        <cfset result=result&Chr(RandRange(65, 90))>
13    </cfloop>
14
15    <!--- Return it --->
16    <cfreturn result>
17</cffunction>

Read More ›