标签:
Console.Write("请输入身份证号:"); string id = Console.ReadLine(); if (id.Length == 18) { string year = id.Substring(6, 4); string month = id.Substring(10, 2); string day = id.Substring(12, 2); Console.WriteLine("您的生日是:" + year + "年" + month + "月" + day + "日"); } else { Console.WriteLine("输入有误!"); } Console.ReadLine();
例:
邮箱格式
1.有且只有一个@
2.不能以@开头
3.@和.不能在一起
4.@后至少有一个.
5.不能以.结尾
Console.Write("请输入你的邮箱账号:"); string mail = Console.ReadLine(); bool a=mail.Contains("@");//有且只能有一个@ if(a==true) { int b=mail.IndexOf ("@");//从前面找@ int c=mail.LastIndexOf("@");//从后面找@ if(b==c)//如果相等那么就有一个@ { if (b != 0)//索引号不等于0 那@不是开头 { string mail1 = mail.Substring(b);//从@开始往后截取为mail1 if (mail1.Contains("."))//是否包含此字符串“.” { int d = mail1.IndexOf(".");//@之后至少有一个 if (d != 1)//@和“.”不能靠在一起 { int e = mail1.LastIndexOf("."); if (e != mail1.Length - 1)//“.”不能是最后一位 { Console.WriteLine("邮箱格式输入正确!"); } else { Console.WriteLine("输入有误!"); } } else { Console.WriteLine("输入有误!"); } } else { Console.WriteLine("输入有误!"); } } else { Console.WriteLine("输入有误。"); } } else { Console.WriteLine("输入有误!"); } } else { Console.WriteLine("输入有误!"); } Console.ReadLine();
例:
随机出验证码,对照输入,判断是否正确
这是我做的!
string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; for (; ; ) { Random ran = new Random(); int a = ran.Next(s.Length-1); int b = ran.Next(s.Length-1); int c = ran.Next(s.Length-1); int d = ran.Next(s.Length-1); string e = (s.Substring(a, 1)); string f = (s.Substring(b, 1)); string g = (s.Substring(c, 1)); string h=(s.Substring(d, 1)); Console.WriteLine(e+f+g+h); Console.Write("请输入验证码:"); string m = Console.ReadLine(); m = m.ToLower(); e = e.ToLower(); f = f.ToLower(); g = g.ToLower(); h = h.ToLower(); if ((e == m.Substring(0, 1)) && (f == m.Substring(1, 1)) && (g == m.Substring(2, 1)) && (h == m.Substring(3, 1))) { Console.Write("输入正确"); break; } else { Console.WriteLine("您输入的验证码有误!请从新输入"); } Console.ReadLine(); Console.Clear(); }
这是老师做的!
string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random ran = new Random(); for (; ; ) { string a = ""; for (int i = 1; i <= 4; i++) { a += s.Substring(ran.Next(s.Length), 1); } Console.WriteLine(a); Console.WriteLine("请输入验证码:"); string b = Console.ReadLine(); if (b.ToLower() == a.ToLower()) { Console.WriteLine("输入正确"); break; } else { Console.Clear(); Console.WriteLine("输入错误"); } } Console.ReadLine();
标签:
原文地址:http://www.cnblogs.com/dianfu123/p/5271207.html