目录
一、使用的方法
1.正则表达式
在注册用户时,经常需要填写密码信息,为保证用户信息的安全性,密码一般情况下要求输入6位或6位以上。本例使用正则表达式可以验证密码长度,密码的长度为6~18位。使用正则表达式Regex类的IsMatch方法,可以有效地判断用户输入的信息是否为数字。
用于验证密码长度的正则表达式可以是:^[A-Za-z0-9]{6,18}$,其中,[A-Za-z0-9]表示大写、小写、数字混合的6~18位密码,不分先后顺序并且至少包含大写、小写、数字中的一种。
2.Char.IsLetterOrDigit方法
先用ToCharArray()静态方法把输入的字符串转成字符数组,再对数组遍历并用Char.IsLetterOrDigit()方法判断数组中是否包含非非法字符,再对合法的密码的长度进行判断。
下面分享源码:
二、源码
1.源代码
// 用正则表达式验证密码长度
// 用Char.IsLetterOrDigit方法遍历字符数组验证密码正确性、长度
namespace _079
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private TextBox? textBox1;
private Button? button1;
private Label? label1;
private Button? button2;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(146, 17),
Name = "textBox1",
Size = new Size(100, 23),
TabIndex = 2
};
//
// button1
//
button1 = new Button
{
Location = new Point(171, 44),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 1,
Text = "验证1",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(35, 23),
Name = "label1",
Size = new Size(80, 17),
TabIndex = 0,
Text = "输入密码:"
};
//
// button2
//
button2 = new Button
{
Location = new Point(171, 71),
Name = "button2",
Size = new Size(75, 23),
TabIndex = 3,
Text = "验证2",
UseVisualStyleBackColor = true
};
button2.Click += Button2_Click;
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(280, 100),
TabIndex = 0,
TabStop = false,
Text = "验证密码长度"
};
groupBox1.Controls.Add(button2);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(304, 123);
Controls.Add(groupBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "正则表达式验证密码长度";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
}
/// <summary>
/// 6~18位大小字母数字组合的混合密码,
/// 字母数字不分先后,且至少要有一种,
/// </summary>
private void Button1_Click(object? sender, EventArgs e)
{
if (!IsPasswLength(textBox1!.Text.Trim()))
{
MessageBox.Show("密码长度不正确或包含非法字符", "验证1");
}
else
{
MessageBox.Show("密码长度正确", "验证1");
}
}
/// <summary>
/// 先用ToCharArray()方法把输入的字符串转成字符数组
/// 再对字符数组遍历,用Char.IsLetterOrDigit()方法判断数组中是否字母数字组合
/// </summary>
private void Button2_Click(object? sender, EventArgs e)
{
int length = textBox1!.Text.Length;
char[] charArr = textBox1!.Text.ToCharArray();
foreach (char c in charArr)
{
if (!Char.IsLetterOrDigit(c))
{
MessageBox.Show("密码中包含非法字符", "验证2");
return;
}
}
if (length >= 6 && length <= 18)
{
MessageBox.Show("密码长度正确", "验证2");
}
else
{
MessageBox.Show("密码长度不正确", "验证2");
}
}
/// <summary>
/// 验证密码长度是否6~18位字母数字混合
/// </summary>
/// <param name="str_Length">用户输入的字符串</param>
/// <returns>方法返回布尔值</returns>
public static bool IsPasswLength(string str_Length)
{
return MyRegex().IsMatch(str_Length);
}
[System.Text.RegularExpressions.GeneratedRegex(@"^[A-Za-z0-9]{6,18}$")]
private static partial System.Text.RegularExpressions.Regex MyRegex();
}
}
2.生成效果