Mega Code Archive

 
Categories / VB.Net Tutorial / GUI
 

Validate user information using regular expressions

Imports System.Windows.Forms Imports System.Text.RegularExpressions public class RegxValidateTextBox    public Shared Sub Main         Application.Run(New FrmValid)    End Sub End class Public Class FrmValid    Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code "    Public Sub New()       MyBase.New()       'This call is required by the Windows Form Designer.       InitializeComponent()       'Add any initialization after the InitializeComponent() call    End Sub    'Form overrides dispose to clean up the component list.    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)       If disposing Then          If Not (components Is Nothing) Then             components.Dispose()          End If       End If       MyBase.Dispose(disposing)    End Sub    Friend WithEvents txtState As System.Windows.Forms.TextBox    Friend WithEvents txtLast As System.Windows.Forms.TextBox    Friend WithEvents txtFirst As System.Windows.Forms.TextBox    Friend WithEvents lblZip As System.Windows.Forms.Label    Friend WithEvents lblPhone As System.Windows.Forms.Label    Friend WithEvents lblLast As System.Windows.Forms.Label    Friend WithEvents lblFirst As System.Windows.Forms.Label    Friend WithEvents lblAddress As System.Windows.Forms.Label    Friend WithEvents txtAddress As System.Windows.Forms.TextBox    Friend WithEvents txtZip As System.Windows.Forms.TextBox    Friend WithEvents txtCity As System.Windows.Forms.TextBox    Friend WithEvents lblCity As System.Windows.Forms.Label    Friend WithEvents lblState As System.Windows.Forms.Label    Friend WithEvents txtPhone As System.Windows.Forms.TextBox    Friend WithEvents cmdOK As System.Windows.Forms.Button    'Required by the Windows Form Designer    Private components As System.ComponentModel.Container    'NOTE: The following procedure is required by the Windows Form Designer    'It can be modified using the Windows Form Designer.      'Do not modify it using the code editor.    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()       Me.txtState = New System.Windows.Forms.TextBox()       Me.txtLast = New System.Windows.Forms.TextBox()       Me.txtFirst = New System.Windows.Forms.TextBox()       Me.lblZip = New System.Windows.Forms.Label()       Me.lblPhone = New System.Windows.Forms.Label()       Me.lblLast = New System.Windows.Forms.Label()       Me.lblFirst = New System.Windows.Forms.Label()       Me.lblAddress = New System.Windows.Forms.Label()       Me.txtAddress = New System.Windows.Forms.TextBox()       Me.txtZip = New System.Windows.Forms.TextBox()       Me.txtCity = New System.Windows.Forms.TextBox()       Me.lblCity = New System.Windows.Forms.Label()       Me.lblState = New System.Windows.Forms.Label()       Me.txtPhone = New System.Windows.Forms.TextBox()       Me.cmdOK = New System.Windows.Forms.Button()       Me.SuspendLayout()       '       'txtState       '       Me.txtState.Location = New System.Drawing.Point(100, 170)       Me.txtState.Name = "txtState"       Me.txtState.Size = New System.Drawing.Size(136, 20)       Me.txtState.TabIndex = 4       Me.txtState.Text = ""       '       'txtLast       '       Me.txtLast.Location = New System.Drawing.Point(100, 30)       Me.txtLast.Name = "txtLast"       Me.txtLast.Size = New System.Drawing.Size(136, 20)       Me.txtLast.TabIndex = 0       Me.txtLast.Text = ""       '       'txtFirst       '       Me.txtFirst.Location = New System.Drawing.Point(100, 65)       Me.txtFirst.Name = "txtFirst"       Me.txtFirst.Size = New System.Drawing.Size(136, 20)       Me.txtFirst.TabIndex = 1       Me.txtFirst.Text = ""       '       'lblZip       '       Me.lblZip.Location = New System.Drawing.Point(10, 205)       Me.lblZip.Name = "lblZip"       Me.lblZip.Size = New System.Drawing.Size(80, 24)       Me.lblZip.TabIndex = 13       Me.lblZip.Text = "Zip"       Me.lblZip.TextAlign = System.Drawing.ContentAlignment.MiddleRight       '       'lblPhone       '       Me.lblPhone.Location = New System.Drawing.Point(10, 240)       Me.lblPhone.Name = "lblPhone"       Me.lblPhone.Size = New System.Drawing.Size(80, 24)       Me.lblPhone.TabIndex = 14       Me.lblPhone.Text = "Phone"       Me.lblPhone.TextAlign = System.Drawing.ContentAlignment.MiddleRight       '       'lblLast       '       Me.lblLast.Location = New System.Drawing.Point(10, 30)       Me.lblLast.Name = "lblLast"       Me.lblLast.Size = New System.Drawing.Size(80, 24)       Me.lblLast.TabIndex = 8       Me.lblLast.Text = "Last Name"       Me.lblLast.TextAlign = System.Drawing.ContentAlignment.MiddleRight       '       'lblFirst       '       Me.lblFirst.Location = New System.Drawing.Point(10, 65)       Me.lblFirst.Name = "lblFirst"       Me.lblFirst.Size = New System.Drawing.Size(80, 24)       Me.lblFirst.TabIndex = 9       Me.lblFirst.Text = "First Name"       Me.lblFirst.TextAlign = System.Drawing.ContentAlignment.MiddleRight       '       'lblAddress       '       Me.lblAddress.Location = New System.Drawing.Point(10, 100)       Me.lblAddress.Name = "lblAddress"       Me.lblAddress.Size = New System.Drawing.Size(80, 24)       Me.lblAddress.TabIndex = 10       Me.lblAddress.Text = "Address"       Me.lblAddress.TextAlign = System.Drawing.ContentAlignment.MiddleRight       '       'txtAddress       '       Me.txtAddress.Location = New System.Drawing.Point(100, 100)       Me.txtAddress.Name = "txtAddress"       Me.txtAddress.Size = New System.Drawing.Size(136, 20)       Me.txtAddress.TabIndex = 2       Me.txtAddress.Text = ""       '       'txtZip       '       Me.txtZip.Location = New System.Drawing.Point(100, 205)       Me.txtZip.Name = "txtZip"       Me.txtZip.Size = New System.Drawing.Size(136, 20)       Me.txtZip.TabIndex = 5       Me.txtZip.Text = ""       '       'txtCity       '       Me.txtCity.Location = New System.Drawing.Point(100, 135)       Me.txtCity.Name = "txtCity"       Me.txtCity.Size = New System.Drawing.Size(136, 20)       Me.txtCity.TabIndex = 3       Me.txtCity.Text = ""       '       'lblCity       '       Me.lblCity.Location = New System.Drawing.Point(10, 135)       Me.lblCity.Name = "lblCity"       Me.lblCity.Size = New System.Drawing.Size(80, 24)       Me.lblCity.TabIndex = 11       Me.lblCity.Text = "City"       Me.lblCity.TextAlign = System.Drawing.ContentAlignment.MiddleRight       '       'lblState       '       Me.lblState.Location = New System.Drawing.Point(10, 170)       Me.lblState.Name = "lblState"       Me.lblState.Size = New System.Drawing.Size(80, 24)       Me.lblState.TabIndex = 12       Me.lblState.Text = "State"       Me.lblState.TextAlign = System.Drawing.ContentAlignment.MiddleRight       '       'txtPhone       '       Me.txtPhone.Location = New System.Drawing.Point(100, 240)       Me.txtPhone.Name = "txtPhone"       Me.txtPhone.Size = New System.Drawing.Size(136, 20)       Me.txtPhone.TabIndex = 6       Me.txtPhone.Text = ""       '       'cmdOK       '       Me.cmdOK.Location = New System.Drawing.Point(112, 296)       Me.cmdOK.Name = "cmdOK"       Me.cmdOK.TabIndex = 15       Me.cmdOK.Text = "OK"       '       'FrmValid       '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)       Me.ClientSize = New System.Drawing.Size(312, 341)       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblPhone, Me.lblZip, Me.lblState, Me.lblCity, Me.lblAddress, Me.lblFirst, Me.lblLast, Me.cmdOK, Me.txtPhone, Me.txtZip, Me.txtState, Me.txtCity, Me.txtAddress, Me.txtFirst, Me.txtLast})       Me.Name = "FrmValid"       Me.Text = "Validate"       Me.ResumeLayout(False)    End Sub #End Region    Private Sub cmdOK_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles cmdOK.Click       If Not Regex.Match(txtLast.Text, "^[A-Z][a-zA-Z]*$").Success Then          txtLast.Text = "Invalid Last Name"          txtLast.Focus()          Return       End If       If Not Regex.Match(txtFirst.Text, "^[A-Z][a-zA-Z]*$").Success Then          txtFirst.Text = "Invalid First Name"          txtFirst.Focus()          Return       End If       If Not Regex.Match(txtAddress.Text, "^[0-9]+\s+([a-zA-Z]" & "+|[a-zA-Z]+\s[a-zA-Z]+)$").Success Then          txtAddress.Text = "Invalid Address"          txtAddress.Focus()          Return       End If       If Not Regex.Match(txtCity.Text, "^([a-zA-Z]+|[a-zA-Z]" & "+\s[a-zA-Z]+)$").Success Then          txtCity.Text = "Invalid City"          txtCity.Focus()          Return       End If       If Not Regex.Match(txtState.Text,"^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$").Success Then          txtState.Text = "Invalid State"          txtState.Focus()          Return       End If       If Not Regex.Match(txtZip.Text, "^\d{5}$").Success Then          txtZip.Text = "Invalid zip code"          txtZip.Focus()          Return       End If       If Not Regex.Match(txtPhone.Text, "^[1-9]" & "\d{2}-[1-9]\d{2}-\d{4}$").Success Then          txtPhone.Text = "Invalid Phone Number"          txtPhone.Focus()          Return       End If       Me.Hide()       Application.Exit()    End Sub ' cmdOK_Click End Class