CFLib.org – Common Function Library Project

RandString(Type, Length)

Last updated November 4, 2003
Download UDF

author

Joshua Miller

Version: 2 | Requires: CF5 | 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:

<CFOUTPUT>
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:

/**
 * 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Mosh Teitelbaum added
minutesToUtcOffs...
19 days ago

Mosh Teitelbaum added
utcOffsetToMinut...
19 days ago

Hank van Empel added
validateIBAN
20 days ago

CF Ninja added
calcIRR
2 months ago

Stephen Withington added
getWeekOfMonth
2 months ago

Created by Raymond Camden / Design by Justin Johnson