3/18小作业,简单的非法输入检测与测试

要求:输入允许1到6个英文字符或数字,按OK结束。

有效等价类为 长度1-6,字符A-Z,a-z,0-9

无效等价类为 长度0,7,字符除以上外的其它字符

测试用例如下

编号 输入 预期结果 结果
test1 Aa3456 OK! 符合
test2 @12测试 ilegal input! 符合
test3   length wrong! 符合
test4 1234567 length wrong! 符合

*注:test3为无输入。

屏幕截图如下

主要代码如下

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"^[A-Za-z0-9]+$");
            String text = textBox1.Text;
            String result;
            if (text.Length > 6 || text.Length < 1)
            {
                result="length wrong!";
            }
            else if (!regex.IsMatch(text))
            {
                result="ilegal input!";
            }
            else
            {
                result="OK!";
            }

            label2.Text = result;
        }
    }
}
View Code

全部文件如下(vs2013)

https://files.cnblogs.com/files/limiting/WindowsFormsApplication3.rar

原文地址:https://www.cnblogs.com/limiting/p/4349932.html