• 软件测试——C#判断密码是否符合要求(二)


    【上次博客中的这个小程序,有一部分功能和代码尚未实现,

    今天将实现剩余功能_(:зゝ∠)_】

    ————————————————我是分界线————————————————

    关键代码

    (?=([x21-x7e]+)[^a-zA-Z0-9])这个正则表达式匹配所有键盘上

    可见的非字母和数字的符号。

    ————————————————我是分界线————————————————

    为了测试方便和使代码更加简洁直观,修改代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;

    namespace SoftwareTest_StrongKey
    {
      public partial class Form1 : Form
      {
        string key_input = null;

        public String IsGoodKey(string key)

        {
          int strLen = key.Length;

          string trim = Regex.Replace(key, @"s", "");
          int strLen2 = trim.Length;
          if (strLen != strLen2)
            return "HAS_BLANK";

          if (strLen <= 5 || strLen > 16)
            return "WRONG_LENGTH";

          Regex r_num = new Regex(@"d+");
          if (!r_num.IsMatch(key))
            return "NO_NUM";

          Regex r_let = new Regex(@"[a-z]+");
          if (!r_let.IsMatch(key))
            return "NO_LET";

          Regex r_cap = new Regex(@"[A-Z]+");
          if (!r_cap.IsMatch(key))
            return "NO_CAP";

          Regex r_spe = new Regex(@"(?=([x21-x7e]+)[^a-zA-Z0-9])+");
          if (!r_spe.IsMatch(key))
            return "NO_SPE";

          return "GOOD_KEY";

        }

        public Form1()
        {
          InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox_key_TextChanged(object sender, EventArgs e)
        {
          key_input = this.textBox_key.Text;
        }

        private void textBox_keyPrompt_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox_name_TextChanged(object sender, EventArgs e)
        {

        }

        private void button_ok_Click(object sender, EventArgs e)
        {
          textBox_keyPrompt.Clear();

          String resStr = IsGoodKey(key_input);
          switch (resStr)
          {
            case "HAS_BLANK":
              textBox_keyPrompt.AppendText("请勿输入空白字符:(");
              break;
            case "WRONG_LENGTH":
              textBox_keyPrompt.AppendText("密码长度不合要求:(");
              break;
            case "NO_NUM":
              textBox_keyPrompt.AppendText("密码须包含数字:(");
              break;
            case "NO_LET":
              textBox_keyPrompt.AppendText("密码须包含小写字母:(");
              break;
            case "NO_CAP":
              textBox_keyPrompt.AppendText("密码须包含大写字母:(");
              break;
            case "NO_SPE":
              textBox_keyPrompt.AppendText("密码须包含特殊字符:(");
              break;
            case "GOOD_KEY":
              textBox_keyPrompt.AppendText("这是一个很棒的密码:)");
              break;
          }  
        }
      }
    }

    ————————————————我是分界线————————————————

    运行结果

    ————————————————我是分界线————————————————

     可见通过正则表达式我们能很好地判断用户输入的密码是否符合要求(๑ŐдŐ)b

  • 相关阅读:
    Spring入门学习(一)
    Debian环境下vi设置
    Debian 环境下安装Tomcat记录
    Debian安装记录
    [原]Fedora 20的yum配置
    vue中slot以及mate的用法
    Express+Nodejs 下的登录拦截实现
    Node.js 常用Mongoose方法
    Node.js常用express方法
    node、Mongo项目如何前后端分离提供接口给前端
  • 原文地址:https://www.cnblogs.com/baishusama/p/4458207.html
Copyright © 2020-2023  润新知