📄 validate.cs
字号:
//
this.addressTextBox.Location = new System.Drawing.Point(100, 100);
this.addressTextBox.Name = "addressTextBox";
this.addressTextBox.Size = new System.Drawing.Size(136, 20);
this.addressTextBox.TabIndex = 18;
this.addressTextBox.Text = "";
//
// firstTextBox
//
this.firstTextBox.Location = new System.Drawing.Point(100, 65);
this.firstTextBox.Name = "firstTextBox";
this.firstTextBox.Size = new System.Drawing.Size(136, 20);
this.firstTextBox.TabIndex = 17;
this.firstTextBox.Text = "";
//
// lastTextBox
//
this.lastTextBox.Location = new System.Drawing.Point(100, 30);
this.lastTextBox.Name = "lastTextBox";
this.lastTextBox.Size = new System.Drawing.Size(136, 20);
this.lastTextBox.TabIndex = 16;
this.lastTextBox.Text = "";
//
// validateForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(312, 341);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.phoneLabel,
this.zipLabel,
this.stateLabel,
this.cityLabel,
this.addressLabel,
this.firstLabel,
this.lastLabel,
this.OkButton,
this.phoneTextBox,
this.zipTextBox,
this.stateTextBox,
this.cityTextBox,
this.addressTextBox,
this.firstTextBox,
this.lastTextBox});
this.Name = "validateForm";
this.Text = "Validate";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new validateForm());
}
// handles OkButton Click event
private void OkButton_Click(object sender, System.EventArgs e)
{
// ensures no textboxes are empty
if ( lastTextBox.Text == "" || firstTextBox.Text == "" ||
addressTextBox.Text == "" || cityTextBox.Text == "" ||
stateTextBox.Text == "" || zipTextBox.Text == "" ||
phoneTextBox.Text == "" )
{
// display popup box
MessageBox.Show( "Please fill in all fields", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
// set focus to lastTextBox
lastTextBox.Focus();
return;
}
// if last name format invalid show message
if ( !Regex.Match( lastTextBox.Text,
@"^[A-Z][a-zA-Z]*$" ).Success )
{
// last name was incorrect
MessageBox.Show( "Invalid Last Name", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Error );
lastTextBox.Focus();
return;
}
// if first name format invalid show message
if ( !Regex.Match( firstTextBox.Text,
@"^[A-Z][a-zA-Z]*$" ).Success )
{
// first name was incorrect
MessageBox.Show( "Invalid First Name", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Error );
firstTextBox.Focus();
return;
}
// if address format invalid show message
if ( !Regex.Match( addressTextBox.Text,
@"^[0-9]+\s+([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$" ).Success )
{
// address was incorrect
MessageBox.Show( "Invalid Address", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Error );
addressTextBox.Focus();
return;
}
// if city format invalid show message
if ( !Regex.Match( cityTextBox.Text,
@"^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$" ).Success )
{
// city was incorrect
MessageBox.Show( "Invalid City", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Error );
cityTextBox.Focus();
return;
}
// if state format invalid show message
if ( !Regex.Match( stateTextBox.Text,
@"^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$" ).Success )
{
// state was incorrect
MessageBox.Show( "Invalid State", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Error );
stateTextBox.Focus();
return;
}
// if zip code format invalid show message
if ( !Regex.Match( zipTextBox.Text, @"^\d{5}$" ).Success )
{
// zip was incorrect
MessageBox.Show( "Invalid Zip Code", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Error );
zipTextBox.Focus();
return;
}
// if phone number format invalid show message
if ( !Regex.Match( phoneTextBox.Text,
@"^[1-9]\d{2}-[1-9]\d{2}-\d{4}$" ).Success )
{
// phone number was incorrect
MessageBox.Show( "Invalid Phone Number", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Error );
phoneTextBox.Focus();
return;
}
// information is valid, signal user and exit application
this.Hide();
MessageBox.Show( "Thank You!", "Information Correct",
MessageBoxButtons.OK, MessageBoxIcon.Information );
Application.Exit();
} // end method OkButton_Click
} // end class validateForm
}
/*
**************************************************************************
* (C) Copyright 2002 by Deitel & Associates, Inc. and Prentice Hall. *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -