C#用正则表达式验证密码长度vs用Char.IsLetterOrDigit方法遍历字符数组验证密码长度

目录

一、使用的方法

1.正则表达式 

2.Char.IsLetterOrDigit方法

二、源码 

1.源代码 

2.生成效果


一、使用的方法

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.生成效果

 

 

 

 

最近更新

  1. TCP协议是安全的吗?

    2024-02-01 11:56:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-01 11:56:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-01 11:56:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-01 11:56:04       20 阅读

热门阅读

  1. 【NGINX】NGINX如何阻止指定ip的请求

    2024-02-01 11:56:04       27 阅读
  2. 【issue-halcon例程学习】rim_simple.hdev

    2024-02-01 11:56:04       31 阅读
  3. 深度学习有何新进展?

    2024-02-01 11:56:04       32 阅读
  4. React和Vue实现路由懒加载

    2024-02-01 11:56:04       27 阅读
  5. SpringCloud

    2024-02-01 11:56:04       37 阅读
  6. 大数据之 Spark 比 MapReduce 快的原因

    2024-02-01 11:56:04       32 阅读
  7. Springboot项目多数据源配置详细步骤

    2024-02-01 11:56:04       33 阅读
  8. 幻兽帕鲁服务器游戏版本怎么升级更新?

    2024-02-01 11:56:04       33 阅读
  9. 低代码开发在金融系统中的应用研究

    2024-02-01 11:56:04       42 阅读
  10. 【Linux 无网络状态下离线安装 MySQL】

    2024-02-01 11:56:04       38 阅读
  11. vue3兼容超宽屏、超窄屏、4K屏幕等等

    2024-02-01 11:56:04       32 阅读