本文介绍了生成验证码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何生成验证码并获取字符串中的值
how to generate verification code and take the value in string
推荐答案
using System.Text;
using System.IO;
static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
private string RandomString(int size)// for random alphabets
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
static class RandomUtil // for random alphanumerics
{
/// <summary>
/// Get random string of 11 characters.
/// </summary>
/// <returns>Random string.</returns>
public static string GetRandomString()
{
string path = Path.GetRandomFileName();
path = path.Replace(".", ""); // Remove period.
return path;
}
}
// random function where ever you wanted
Label1.Text = RandomUtil.GetRandomString(); // for alphanumeric
Label2.Text = RandomString(10); // for alphanumeric of length 10
Label3.Text = RandomNumber(10,10000); // for random number between 10 and 10000
请参阅以下链接..
[]
这篇关于生成验证码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!