.net winform 数据验证框架

来源:互联网 发布:网络主播经常唱的歌 编辑:程序博客网 时间:2024/05/24 03:30
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using com.v246.validation;namespace TestValidation{    public partial class Form1 : Form    {        private AqucyValidations av = null;        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            av = new AqucyValidations();            StringValidation sv = new StringValidation(userName, "Text");            sv.MinLength = 2;            sv.MaxLength = 4;            sv.MinLengthString = "姓名不能小于2个字符";            sv.MaxLengthString = "姓名不能大于5个字符";            sv.Required = true;            av.add(sv);            NumberValidation nv = new NumberValidation(age, "Text");            nv.IsReal = false;            nv.MaxValue = "120";            nv.MinValue = "15";            nv.Required = true;            av.add(nv);            RegexValidation rv = new RegexValidation(phone, "Text");            rv.Required = true;            rv.RegexValue = "((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";            av.add(rv);            rv = new RegexValidation(email, "Text");            rv.RegexValue = "^[a-z]([a-z0-9]*[-_\\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\\.][a-z]{2,3}([\\.][a-z]{2})?{1}quot;;            rv.ErrorString = "您所输入的Email地址不合法";            av.add(rv);            av.ValidationFaild += new EventHandler(av_ValidationFaild);            av.ValidationPass += new EventHandler(av_ValidationPass);            av.validationAll();        }        void av_ValidationPass(object sender, EventArgs e)        {            button1.Enabled = true;        }        void av_ValidationFaild(object sender, EventArgs e)        {            button1.Enabled = false;        }        private void button1_Click(object sender, EventArgs e)        {            if (av.validationAll() == 0)            {                MessageBox.Show("验正成功");            }            else            {                MessageBox.Show("验证失败");            }        }    }}

效果如下:


Demo下载地址:http://download.csdn.net/source/3522303

只要将bin下的AqucyLib.dll引用至您的工程内即可使用