1. 程式人生 > >網站登錄簡單驗證碼

網站登錄簡單驗證碼

ted car 重復 new load send bin col bind

 1  public string BindCode(int length)
 2         {
 3             string Vchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,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,P,Q,R,S,T,U,V,W,X,Y,Z";
 4 
 5             string[] VcArray = Vchar.Split(new Char[] { , });         
 8             Random rand = new
Random(); 10 11 int a; 12 List<int> result = new List<int>(); 13 string numlist = string.Empty; 14 15 for (int i = 1; i < length + 1; i++) 16 17 { 18 a = rand.Next(1, VcArray.Length); //設置範圍。我這裏是取數據庫的數據1~n
19 if (result.Contains(a)) 20 { 21 i = i - 1; //有重復值,重新再來一次,循環次數減一次 22 } 23 else 24 { 25 numlist += VcArray[a]; //無重復值,就添加在集合內 26 } 27 } 28 29 return
numlist; 30 }

調用:

1  protected void Page_Load(object sender, EventArgs e)
2         {
3             if (!IsPostBack)
4             {
5                 Barcoded.Text = BindCode(4);
6             }
7         }

網站登錄簡單驗證碼