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 DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Controls;
using System.Text.RegularExpressions; namespace DevDemo
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
InitValidationMode();
} private void InitValidationMode()
{
CustomValidationRule rule = new CustomValidationRule();
rule.ErrorText = "用户登录代码只能是8位长,并且前两位是字符,后面是数字";
rule.ErrorType = ErrorType.Critical;
//绑定验证控件
vpMain.SetValidationRule(txtUserName, rule);
} private void btnLogin_Click(object sender, EventArgs e)
{
vpMain.Validate();
if (txtUserName.Text == "vp123456" && txtPassword.Text == MD5("aaaaaa"))
{
this.Hide();
MainForm m = new MainForm();
m.Show();
}
} private string MD5(string str)
{
return str;
}
} #region CustomValidationRule
// <customTextEdit>
public class CustomValidationRule : ValidationRule
{
public override bool Validate(Control control, object value)
{
string parttern = @"^[\w\W]{2}\d{6}$";
bool res = false;
res = Regex.IsMatch(value.ToString(), parttern);
return res;
}
} #endregion
}

LoginForm.cs

namespace DevDemo
{
partial class LoginForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.btnLogin = new DevExpress.XtraEditors.SimpleButton();
this.lblUserName = new DevExpress.XtraEditors.LabelControl();
this.lblPassword = new DevExpress.XtraEditors.LabelControl();
this.txtUserName = new DevExpress.XtraEditors.TextEdit();
this.vpMain = new DevExpress.XtraEditors.DXErrorProvider.DXValidationProvider(this.components);
this.txtPassword = new DevExpress.XtraEditors.TextEdit();
((System.ComponentModel.ISupportInitialize)(this.txtUserName.Properties)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.vpMain)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.txtPassword.Properties)).BeginInit();
this.SuspendLayout();
//
// btnLogin
//
this.btnLogin.Location = new System.Drawing.Point(, );
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(, );
this.btnLogin.TabIndex = ;
this.btnLogin.Text = "登录";
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
//
// lblUserName
//
this.lblUserName.Location = new System.Drawing.Point(, );
this.lblUserName.Name = "lblUserName";
this.lblUserName.Size = new System.Drawing.Size(, );
this.lblUserName.TabIndex = ;
this.lblUserName.Text = "用户名";
//
// lblPassword
//
this.lblPassword.Location = new System.Drawing.Point(, );
this.lblPassword.Name = "lblPassword";
this.lblPassword.Size = new System.Drawing.Size(, );
this.lblPassword.TabIndex = ;
this.lblPassword.Text = "密 码";
//
// txtUserName
//
this.vpMain.SetIconAlignment(this.txtUserName, System.Windows.Forms.ErrorIconAlignment.MiddleRight);
this.txtUserName.Location = new System.Drawing.Point(, );
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(, );
this.txtUserName.TabIndex = ;
//
// txtPassword
//
this.vpMain.SetIconAlignment(this.txtPassword, System.Windows.Forms.ErrorIconAlignment.MiddleRight);
this.txtPassword.Location = new System.Drawing.Point(, );
this.txtPassword.Name = "txtPassword";
this.txtPassword.Properties.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(, );
this.txtPassword.TabIndex = ;
//
// LoginForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.lblPassword);
this.Controls.Add(this.lblUserName);
this.Controls.Add(this.btnLogin);
this.Name = "LoginForm";
this.Text = "登陆";
((System.ComponentModel.ISupportInitialize)(this.txtUserName.Properties)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.vpMain)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.txtPassword.Properties)).EndInit();
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private DevExpress.XtraEditors.SimpleButton btnLogin;
private DevExpress.XtraEditors.LabelControl lblUserName;
private DevExpress.XtraEditors.LabelControl lblPassword;
private DevExpress.XtraEditors.TextEdit txtUserName;
private DevExpress.XtraEditors.DXErrorProvider.DXValidationProvider vpMain;
private DevExpress.XtraEditors.TextEdit txtPassword;
}
}

LoginForm.Designer.cs

-----------------------------

天王盖地虎小说网:http://www.twgdh.com/

 

winform 表单正则表达式验证 示例(ValidationRule)的更多相关文章

  1. thinkPHP 表单自动验证功能

    昨天晚上我们老大叫我弄表单自动验证功能,愁了半天借鉴了好多官网的知识,才出来,诶,总之分享一下我自己的成果吧! thinkphp 在Model基类为我们定义了自动验证的函数和正则表达式,我们只需要在对 ...

  2. thinkphp表单自动验证

    ThinkPHP框架表单验证 对注册到test表的表单进行验证 在注册之前要对表单进行验证: 用户名非空验证,两次输入密码必须一致即相等验证,年龄在18~50之间即范围验证,邮箱格式正则验证. 自动验 ...

  3. aspx中的表单验证 jquery.validate.js 的使用 以及 jquery.validate相关扩展验证(Jquery表单提交验证插件)

    这一期我们先讲在aspx中使用 jquery.validate插件进行表单的验证, 关于MVC中使用 validate我们在下一期中再讲     上面是效果,下面来说使用步骤 jQuery.Valid ...

  4. PHP - 表单与验证

    第11章 表单与验证 学习要点: 1.Header()函数 2.接收及验证数据 我们对Web感兴趣,认为它有用的原因是其主要通过基于HTML的表单发布和收集信息的能力.这些表单用来鼓励网站的反馈.进行 ...

  5. HTML5表单及其验证

    随笔- 15 文章- 1 评论- 115 HTML5表单及其验证   HTML表单一直都是Web的核心技术之一,有了它我们才能在Web上进行各种各样的应用.HTML5 Forms新增了许多新控件及其A ...

  6. HTML的表单初级验证

    HTML的表单初级验证 placeholder(提示信息) required(确保不能为空) pattern(正则表达式验证) 1. placeholder(提示信息) 语法: <p>账号 ...

  7. Go-Web编程_表单_0x02_验证表单的输入

    开发Web的一个原则就是,不能信任用户输入的任何信息,所以验证和过滤用户的输入信息就变得非常重要,我们经常会在微博.新闻中听到某某网站被入侵了,存在什么漏洞,这些大多是因为网站对于用户输入的信息没有做 ...

  8. bootstrap表单带验证

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  9. easyui表单多重验证,动态设置easyui控件

    要实现的功能:在做添加学生信息的时候,利用easyui的验证功能判断 学号是否重复和学号只能为数字 最终效果如下图: 但在做这个的过程中,遇到了一系列的问题: 扩展validatebox的验证方法,最 ...

随机推荐

  1. linux 文本处理

    tr,awk,sed 一:tr 1.大小写转换 cat file | tr [a-z] [A-Z] > new_file(大写 --> 小写) cat file | tr [A-Z] [a ...

  2. Linux Hugetlbfs内核源码简析-----(二)Hugetlbfs挂载

    本文只讨论执行"mount none /mnt/huge -t hugetlbfs"命令后,mount系统调用的执行过程(基于Linux-3.4.51),不涉及进程相关的细节. m ...

  3. ADO.NET(很精彩全面)

    记录一下地址方便以后看http://www.cnblogs.com/liuhaorain/archive/2012/02/06/2340409.html

  4. C# 随机颜色的方法

    public string GetRandomColor() { Random RandomNum_First = new Random((int)DateTime.Now.Ticks); // 对于 ...

  5. Java TCP异步数据接收

    之前一直采用.Net编写服务端程序,最近需要切换到Linux平台下,于是尝试采用Java编写数据服务器.TCP异步连接在C#中很容易实现,网上也有很多可供参考的代码.但Java异步TCP的参考资料较少 ...

  6. 仿酒仙网的一款jQuery侧栏弹出导航栏特效

    仿酒仙网的一款jQuery侧栏弹出导航栏特效 一款常用于商城左侧商品导航的jquery菜单导航特效. 非常不错的一款商品分类特效.大家可以拿去研究研究 . 注意:该特效还支持挨千刀的IE6啊,之强大. ...

  7. ASP.NET MVC5学习笔记之Filter提供体系

    前面我们介绍了Filter的基本使用,但各种Filter要在合适的时机运行起来,需要预先准备好,现在看看ASP.NET MVC框架是怎么做的. 一.Filter集合 在ControlerActionI ...

  8. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  9. Python学习教程(learning Python)--2.3.5 Python返回多个值问题

    本节主要学习Python的函数是如何同时返回多个值的问题. 在学习Python的时候惊奇的发现,Python的函数可以同时返回多个值,很有意思. #define function sum def su ...

  10. 用Java简单实现C#的参数为Action<T> Function<T,boolean>扩展方法

    直接上代码 Blog.Java public class Blog { public Blog(int id,String name) { Id=id; Name=name; } public int ...