using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
namespace Validation
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ErrorProvider errorProvider1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Label label6;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(136, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
this.textBox1.Validated += new System.EventHandler(this.textBox_Validated);
//
// errorProvider1
//
this.errorProvider1.DataMember = null;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(136, 56);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2_Validating);
this.textBox2.Validated += new System.EventHandler(this.textBox_Validated);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 2;
this.label1.Text = "a-zのみ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 3;
this.label2.Text = "A-Zのみ";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 16);
this.label3.TabIndex = 4;
this.label3.Text = "0-9のみ";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(136, 96);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 5;
this.textBox3.Text = "textBox3";
this.textBox3.Validating += new System.ComponentModel.CancelEventHandler(this.textBox3_Validating);
this.textBox3.Validated += new System.EventHandler(this.textBox_Validated);
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 136);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 16);
this.label4.TabIndex = 6;
this.label4.Text = "郵便番号のみ";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(136, 136);
this.textBox4.Name = "textBox4";
this.textBox4.TabIndex = 7;
this.textBox4.Text = "textBox4";
this.textBox4.Validating += new System.ComponentModel.CancelEventHandler(this.textBox4_Validating);
this.textBox4.Validated += new System.EventHandler(this.textBox_Validated);
//
// label5
//
this.label5.Location = new System.Drawing.Point(16, 184);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(104, 16);
this.label5.TabIndex = 8;
this.label5.Text = "全角ひらがなのみ";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(136, 176);
this.textBox5.Name = "textBox5";
this.textBox5.TabIndex = 9;
this.textBox5.Text = "textBox5";
this.textBox5.Validating += new System.ComponentModel.CancelEventHandler(this.textBox5_Validating);
this.textBox5.Validated += new System.EventHandler(this.textBox_Validated);
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(136, 216);
this.textBox6.Name = "textBox6";
this.textBox6.TabIndex = 10;
this.textBox6.Text = "textBox6";
this.textBox6.Validating += new System.ComponentModel.CancelEventHandler(this.textBox6_Validating);
this.textBox6.Validated += new System.EventHandler(this.textBox_Validated);
//
// label6
//
this.label6.Location = new System.Drawing.Point(16, 216);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(96, 16);
this.label6.TabIndex = 11;
this.label6.Text = "全角カタカナのみ";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label6,
this.textBox6,
this.textBox5,
this.label5,
this.textBox4,
this.label4,
this.textBox3,
this.label3,
this.label2,
this.label1,
this.textBox2,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
// a-z のみの文字列
// 正しく入力しないと次に行けない
Regex regex = new System.Text.RegularExpressions.Regex(@"^[a-z]+$");
if ( ! regex.IsMatch( ((TextBox)sender).Text ))
{
this.errorProvider1.SetError( (TextBox)sender, "a-z のみの文字列" );
// e.Cancel = true でCancel を true にしてキャンセルする。
e.Cancel = true;
}
}
private void textBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
// A-Z のみの文字列
// 正しく入力してなくても次に行くパターン
Regex regex = new System.Text.RegularExpressions.Regex(@"^[A-Z]+$");
if ( ! regex.IsMatch( ((TextBox)sender).Text ))
{
this.errorProvider1.SetError( (TextBox)sender, "A-Z のみの文字列" );
// e.Cancel = true でCancel を true にしてキャンセルする。
e.Cancel = true;
}
}
private void textBox3_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
// 0-9 のみの文字列
// 正しく入力してなくても次に行くパターン
Regex regex = new System.Text.RegularExpressions.Regex(@"^[0-9]+$");
if ( ! regex.IsMatch( ((TextBox)sender).Text ))
{
this.errorProvider1.SetError( (TextBox)sender, "0-9 のみの文字列" );
e.Cancel = true;
}
}
private void textBox4_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
// 郵便番号
// 正しく入力してなくても次に行くパターン
Regex regex = new System.Text.RegularExpressions.Regex(@"^[0-9]{3}-[0-9]{4}$");
if ( ! regex.IsMatch( ((TextBox)sender).Text ))
{
this.errorProvider1.SetError( (TextBox)sender, "郵便番号" );
e.Cancel = true;
}
}
private void textBox5_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
// 全角ひらがなのみの文字列
// 正しく入力しないと次に行けない
Regex regex = new System.Text.RegularExpressions.Regex(@"^[あ-を]+$");
if ( ! regex.IsMatch( ((TextBox)sender).Text ))
{
this.errorProvider1.SetError( (TextBox)sender, "全角ひらがなのみ" );
// e.Cancel = true でCancel を true にすると正しく入力しないと次に行けない。
e.Cancel = true;
}
}
private void textBox6_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
// 全角カタカナのみの文字列
// 正しく入力しないと次に行けない
Regex regex = new System.Text.RegularExpressions.Regex(@"^[ア-ヲ]+$");
if ( ! regex.IsMatch( ((TextBox)sender).Text ))
{
this.errorProvider1.SetError( (TextBox)sender, "全角カタカナのみ" );
e.Cancel = true;
}
}
private void textBox_Validated(object sender, System.EventArgs e)
{
this.errorProvider1.SetError( (TextBox)sender, null );
}
}
}
|