RandString(Type, Length)
Last updated November 4, 2003
Version: 2 | Requires: ColdFusion 5 | Library: StrLib
Description:
This UDF generates a random string of the specified length of either Alpha, Numeric or mixed Alpha-Numeric characters. Could be useful for username generation, "Key Code" generation or any number of other situations.
Return Values:
Returns a string.
Example:
Random Alpha-Numeric String: #randString('alphanum',8)#<br>
Random Alpha String: #randString('alpha',8)#<br>
Random Numeric String: #randString('numeric',8)#<br>
Random "Secure" String: #randString('secure',10)#<br>
</CFOUTPUT>
Parameters:
| Name | Description | Required |
|---|---|---|
| Type | Type of random string to create. | Yes |
| Length | Length of random string to create. | Yes |
Full UDF Source:
<cfscript>
/**
* Returns a random string of the specified length of either alpha, numeric or mixed-alpha-numeric characters.
* v2, support for lower case
* v3 - more streamlined code
*
* @param Type Type of random string to create. (Required)
* @param Length Length of random string to create. (Required)
* @return Returns a string.
* @author Joshua Miller (josh@joshuasmiller.com)
* @version 2, November 4, 2003
*/
function randString(type,ct){
var i=1;
var randStr="";
var randNum="";
var useList="";
var alpha="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
var secure="!,@,$,%,&,*,-,_,=,+,?,~";
for(i=1;i LTE ct;i=i+1){
if(type is "alpha"){
randNum=RandRange(1,52);
useList=alpha;
}else if(type is "alphanum"){
randNum=RandRange(1,62);
useList="#alpha#,0,1,2,3,4,5,6,7,8,9";
}else if(type is "secure"){
randNum=RandRange(1,73);
useList="#alpha#,0,1,2,3,4,5,6,7,8,9,#secure#";
}else{
randNum=RandRange(1,10);
useList="0,1,2,3,4,5,6,7,8,9";
}
randStr="#randStr##ListGetAt(useList,randNum)#";
}
return randStr;
}
</cfscript>
Search CFLib.org
Latest Additions
Shawn Porter added
DeMoronize
2 hour(s) ago
Chris Carey added
readPropertiesFi...
1 day(s) ago
Randy Johnson added
lastDayofWeek
3 day(s) ago
Frank Marion added
sitemapPing
7 day(s) ago
Top Rated
QuickSort
Rated 5.0, 3 time(s)
indentXml
Rated 5.0, 3 time(s)
queryColumnsToSt...
Rated 5.0, 3 time(s)
generateSsccAsn
Rated 5.0, 3 time(s)