C#英文数字混合验证

日常可见的验证码,当然不会这么简单,不过算是基本验证码中比较经典的,可以做一点参考,欢迎有更好方法的大哥们指正

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _11._1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13 
14             //无限循环,直至用户验证码输入成功
15             while (true)
16             {
17 
18                 //定义一个空的字符串,以便可以放置四个字符串
19                 string yzm1 = "";
20                 //创建随机数
21                 Random ran = new Random();
22 
23 
24                 //循环4次,每次循环,从string x随机提取一位字符串作为验证码,每次提取都放入string yzm中,共提取4次
25                 for (int i = 0; i < 4; i++)
26                 {
27                     //定义一个字符串,里面放上需要使用的随机内容
28                     string x = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPASDFGHJKLMNBVCXZ0987654321";
29                     //从string x中随机提取一个字符串给int str赋值
30                     int j = ran.Next(0, x.Length);
31                     string str = x.Substring(j, 1);
32                     yzm1 += str;
33                 }
34                 Console.WriteLine("验证码:" + yzm1);
35                 Console.Write("验  证:");
36                 //接受用户输入的信息
37                 string user1 = Console.ReadLine();
38                 //防止用户误操作,去掉空格
39                 string user2 = user1.Replace(" ", "");
40                 //防止用户误操作,大小写统一变成小写
41                 string yzm = yzm1.ToLower();
42 
43                 if (user2.Length == 4)
44                 {
45                     string user = user2.ToLower();
46                     if (user == yzm)
47                     {
48                         Console.WriteLine();
49                         Console.ForegroundColor = ConsoleColor.Red;
50                         Console.WriteLine("验证成功!!!");
51                         Console.WriteLine();
52                         Console.ForegroundColor = ConsoleColor.White;
53                         break;
54                     }
55                     else
56                     {
57                         Console.WriteLine();
58                         Console.ForegroundColor = ConsoleColor.Red;
59                         Console.WriteLine("输入错误!!!");
60                         Console.WriteLine();
61                         Console.ForegroundColor = ConsoleColor.White;
62                     }
63                 }
64                 else
65                 {
66                     Console.WriteLine();
67                     Console.ForegroundColor = ConsoleColor.Red;
68                     Console.WriteLine("长度错误!!!");
69                     Console.WriteLine();
70                     Console.ForegroundColor = ConsoleColor.White;
71                 }
72             }
73 
74             Console.ReadLine();
75         }
76     }
77 }

小写可以,大写也可以,代码里面统一转换成小写的

都是随机的,不是很好能遇到全部都是大写,全部都是小写的,不好遇到,各位同学将就看

具体看代码就可以了

原文地址:https://www.cnblogs.com/TheJoker/p/7767453.html