//生成数字加字母的字符串的方法
public static string ToRandPwd()
{
Random random = new Random();
int pwdLength = random.Next(6, 10);//密码长度控制在6到10位之间
string password = "";
string temp1 = string.Empty;
int digitNum1 = random.Next(pwdLength - 1);
if (digitNum1 == 0)
{
digitNum1 = 1;
}
int charUpperNum1 = random.Next(pwdLength - digitNum1);
if (charUpperNum1 == 0)
charUpperNum1 = 1;
int charLowerNum1 = pwdLength - digitNum1 - charUpperNum1;
for (int i = 0; i < digitNum1; i++)
{
temp1 += random.Next(0, 10);//digit
}
for (int i = 0; i < charUpperNum1; i++)
{
temp1 += (char)random.Next(65, 91);//upper case
}
for (int i = 0; i < charLowerNum1; i++)
{
temp1 += (char)random.Next(97, 123);
}
for (int i = 0; i < pwdLength; i++) //disarrange password
{
int n = random.Next(temp1.Length);
password += temp1[n];
temp1 = temp1.Remove(n, 1);
}
return password;
}
using System.Text.RegularExpressions;
if(Regex.IsMatch(this.TextBox1.Text,@"^(?!([^A-Za-z]|\d)+$)[a-zA-Z\d]{6,}$")) //@"^(?!\D+$)(?![^a-z]+$)[a-zA-Z\d]{6,}$"
{
Response.Write("你输入的符合是至少为六位且必须是数字和字母组合的字符串");
}
if (Regex.IsMatch(this.TextBox1.Text, @"^[A-Za-z]+$"))
{
Response.Write("都是字母");
}