C#生成驗證碼之四位隨機數


 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace WindowsFormsApplication2
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18         }
19         private void Form1_Load(object sender, EventArgs e)
20         {
21             string vc = "";
22             Random rNum = new Random();//隨機生成類
23             int num1 = rNum.Next(0, 9);//返回指定范圍內的隨機數
24             int num2 = rNum.Next(0, 9);
25             int num3 = rNum.Next(0, 9);
26             int num4 = rNum.Next(0, 9);
27 
28             int[] nums = new int[4] { num1, num2, num3, num4 };
29             for (int i = 0; i < nums.Length; i++)//循環添加四個隨機生成數
30             {
31                 vc += nums[i].ToString();
32             }
33             lblVerificationCode.Text = vc;
34         }
35         private void btnVerification_Click(object sender, EventArgs e)
36         {
37             if (txtInput.Text != null && txtInput.Text != "")//用戶輸入不為空
38             {
39                 if (txtInput.Text == lblVerificationCode.Text)//判斷用戶輸入與隨機生成的四位數是否相同
40                 {
41                     MessageBox.Show("驗證成功!");
42                 }
43                 else
44                 {
45                     MessageBox.Show("驗證失敗!");
46                 }
47             }
48             else
49             {
50                 MessageBox.Show("請輸入驗證碼!");
51             }
52         }
53 
54         
55     }
56 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM