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. CSS常用布局整理(二)

    1-2-1单列变宽布局 side列定宽300px,content列变宽,尺寸是100%-300px.核心的问题就是浮动列的宽度应该等于“100% - 300px”,而CSS显然不支持这种带有减法运算的 ...

  2. 更改Pch配置

    在Build Settings 里搜索prefix  在第一个AppleLLVM.7.0 有一个 PrefixHeader 属性  在属性里添加 $(SRCROOT)/加上pch文件名字    然后编 ...

  3. sqlmap.config 配置

    <?xml version="1.0" encoding="utf-8"?> <sqlMapConfig xmlns="http:/ ...

  4. leetcode 24

    链表操作的,要注意标记头结点和边界问题. 代码如下: ListNode *swapPairs(ListNode *head) { if(head==NULL||head->next==NULL) ...

  5. s3c6410_时钟初始化

    参考: 1)<USER'S MANUAL-S3C6410X>第三章 SYSTEM CONTROLLER 2)u-boot/board/samsumg/smdk6410/lowlevel_i ...

  6. java Literals

    Primitive Data Types The Java programming language is statically-typed, which means that all variabl ...

  7. 自定义控件winfrom

    附件代码:http://files.cnblogs.com/files/qtiger/CompositeControl.zip 转载:http://www.cnblogs.com/bomo/archi ...

  8. [leetcode]_Symmetric Tree

    第二道树的题目,依旧不会做,谷歌经验. 题目解释: give you a tree , judge if it is a symmetric tree. 思路:我以为要写个中序遍历(进阶学习非递归算法 ...

  9. while循环中不支持循环使用curl

    <?php $link = mysql_connect('localhost', 'sms', 'sms'); mysql_select_db('sms', $link); mysql_quer ...

  10. 3)Java容器

    3)Java容器   Java的集合框架核心主要有三种:List.Set和Map.这里的 Collection.List.Set和Map都是接口(Interface). List lst = new ...