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 = @"^[\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)的更多相关文章
- thinkPHP 表单自动验证功能
昨天晚上我们老大叫我弄表单自动验证功能,愁了半天借鉴了好多官网的知识,才出来,诶,总之分享一下我自己的成果吧! thinkphp 在Model基类为我们定义了自动验证的函数和正则表达式,我们只需要在对 ...
- thinkphp表单自动验证
ThinkPHP框架表单验证 对注册到test表的表单进行验证 在注册之前要对表单进行验证: 用户名非空验证,两次输入密码必须一致即相等验证,年龄在18~50之间即范围验证,邮箱格式正则验证. 自动验 ...
- aspx中的表单验证 jquery.validate.js 的使用 以及 jquery.validate相关扩展验证(Jquery表单提交验证插件)
这一期我们先讲在aspx中使用 jquery.validate插件进行表单的验证, 关于MVC中使用 validate我们在下一期中再讲 上面是效果,下面来说使用步骤 jQuery.Valid ...
- PHP - 表单与验证
第11章 表单与验证 学习要点: 1.Header()函数 2.接收及验证数据 我们对Web感兴趣,认为它有用的原因是其主要通过基于HTML的表单发布和收集信息的能力.这些表单用来鼓励网站的反馈.进行 ...
- HTML5表单及其验证
随笔- 15 文章- 1 评论- 115 HTML5表单及其验证 HTML表单一直都是Web的核心技术之一,有了它我们才能在Web上进行各种各样的应用.HTML5 Forms新增了许多新控件及其A ...
- HTML的表单初级验证
HTML的表单初级验证 placeholder(提示信息) required(确保不能为空) pattern(正则表达式验证) 1. placeholder(提示信息) 语法: <p>账号 ...
- Go-Web编程_表单_0x02_验证表单的输入
开发Web的一个原则就是,不能信任用户输入的任何信息,所以验证和过滤用户的输入信息就变得非常重要,我们经常会在微博.新闻中听到某某网站被入侵了,存在什么漏洞,这些大多是因为网站对于用户输入的信息没有做 ...
- bootstrap表单带验证
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- easyui表单多重验证,动态设置easyui控件
要实现的功能:在做添加学生信息的时候,利用easyui的验证功能判断 学号是否重复和学号只能为数字 最终效果如下图: 但在做这个的过程中,遇到了一系列的问题: 扩展validatebox的验证方法,最 ...
随机推荐
- 【spring 4】AOP:动态代理
一.动态代理简介 动态代理与普通代理相比较,最大的好处是接口中声明的所有方法都被转移到一个集中的方法中处理(invoke),这样,在接口方法数量比较多的时候,我们可以进行灵活处理,而不需要像静态代理那 ...
- [前端 4] 使用Js实现图片上传预览
导读:今天做图片上传预览,刚开始的做法是,先将图片上传到Nginx,然后重新加载页面才能看到这个图片.在这个过程中,用户一直都看不到自己上传的文件是什么样子.Ps:我发现我真的有强迫症了,都告诉我说不 ...
- PAT1007
#include<stdio.h>#include<vector>#include<algorithm>using namespace std; int main( ...
- ipv4、ipv6的socket同时监听“bind: Address already in use”的解决方法
创建ipv4和ipv6的socket,同时监听某个端口的ipv4和ipv6报文,运行时bind函数执行失败,提示“bind: Address already in use”.原因:ipv6的socke ...
- DPDK内存管理-----(一)初始化
1 前言 DPDK通过使用hugetlbfs,减少CPU TLB表的Miss次数,提高性能. 2 初始化 DPDK的内存初始化工作,主要是将hugetlbfs的配置的大内存页,根据其映射的物理地址是否 ...
- html Doctype作用?
Doctype它主要的作用来声明html的版本 <!Doctype html>这是html5的 不写可能会造成html5的功能不能用(具体会不会出错就要看浏览器的容错性)
- css3- border
css3-border 1.border-color 2.border-image 3.border-radius ( none | <length>{1,4} [ / <leng ...
- c# 实现串口编程-操作LED屏幕
串口编程主要用到SerialPort这个类,主要实现对串口发送字节数组然后点阵屏显示相关信息,其实这个功能很简单下面给大家把整体思路用流程图展现如下:. 其实整体思路就如流程图.下面是整个流程图的一个 ...
- Oracle删除表主键语句
删除无名主键语句: SELECT * FROM USER_CONS_COLUMNS WHERE TABLE_NAME = '表名'; ALTER TABLE 表名 DROP CONSTRAINT 主键 ...
- 平衡二叉树(AVL)
性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树. 定义:平衡二叉树或为空树,或为如下性质的二叉排序树: (1)左右子树深度之差的绝对值不超过1; (2 ...