winform 表单正则表达式验证 示例(ValidationRule)

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 = @"^[wW]{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(315, 178);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(75, 23);
this.btnLogin.TabIndex = 0;
this.btnLogin.Text = "登录";
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
//
// lblUserName
//
this.lblUserName.Location = new System.Drawing.Point(72, 55);
this.lblUserName.Name = "lblUserName";
this.lblUserName.Size = new System.Drawing.Size(36, 14);
this.lblUserName.TabIndex = 1;
this.lblUserName.Text = "用户名";
//
// lblPassword
//
this.lblPassword.Location = new System.Drawing.Point(72, 96);
this.lblPassword.Name = "lblPassword";
this.lblPassword.Size = new System.Drawing.Size(40, 14);
this.lblPassword.TabIndex = 1;
this.lblPassword.Text = "密 码";
//
// txtUserName
//
this.vpMain.SetIconAlignment(this.txtUserName, System.Windows.Forms.ErrorIconAlignment.MiddleRight);
this.txtUserName.Location = new System.Drawing.Point(146, 55);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(244, 20);
this.txtUserName.TabIndex = 2;
//
// txtPassword
//
this.vpMain.SetIconAlignment(this.txtPassword, System.Windows.Forms.ErrorIconAlignment.MiddleRight);
this.txtPassword.Location = new System.Drawing.Point(146, 96);
this.txtPassword.Name = "txtPassword";
this.txtPassword.Properties.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(244, 20);
this.txtPassword.TabIndex = 3;
//
// LoginForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(473, 261);
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

原文地址:https://www.cnblogs.com/Echo529/p/6382324.html