Validate user information using regular expressions. : TextBox Validation « GUI « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » GUI » TextBox Validation 
14.24.3.Validate user information using regular expressions.
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 NothingThen
            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(100170)
      Me.txtState.Name = "txtState"
      Me.txtState.Size = New System.Drawing.Size(13620)
      Me.txtState.TabIndex = 4
      Me.txtState.Text = ""
      '
      'txtLast
      '
      Me.txtLast.Location = New System.Drawing.Point(10030)
      Me.txtLast.Name = "txtLast"
      Me.txtLast.Size = New System.Drawing.Size(13620)
      Me.txtLast.TabIndex = 0
      Me.txtLast.Text = ""
      '
      'txtFirst
      '
      Me.txtFirst.Location = New System.Drawing.Point(10065)
      Me.txtFirst.Name = "txtFirst"
      Me.txtFirst.Size = New System.Drawing.Size(13620)
      Me.txtFirst.TabIndex = 1
      Me.txtFirst.Text = ""
      '
      'lblZip
      '
      Me.lblZip.Location = New System.Drawing.Point(10205)
      Me.lblZip.Name = "lblZip"
      Me.lblZip.Size = New System.Drawing.Size(8024)
      Me.lblZip.TabIndex = 13
      Me.lblZip.Text = "Zip"
      Me.lblZip.TextAlign = System.Drawing.ContentAlignment.MiddleRight
      '
      'lblPhone
      '
      Me.lblPhone.Location = New System.Drawing.Point(10240)
      Me.lblPhone.Name = "lblPhone"
      Me.lblPhone.Size = New System.Drawing.Size(8024)
      Me.lblPhone.TabIndex = 14
      Me.lblPhone.Text = "Phone"
      Me.lblPhone.TextAlign = System.Drawing.ContentAlignment.MiddleRight
      '
      'lblLast
      '
      Me.lblLast.Location = New System.Drawing.Point(1030)
      Me.lblLast.Name = "lblLast"
      Me.lblLast.Size = New System.Drawing.Size(8024)
      Me.lblLast.TabIndex = 8
      Me.lblLast.Text = "Last Name"
      Me.lblLast.TextAlign = System.Drawing.ContentAlignment.MiddleRight
      '
      'lblFirst
      '
      Me.lblFirst.Location = New System.Drawing.Point(1065)
      Me.lblFirst.Name = "lblFirst"
      Me.lblFirst.Size = New System.Drawing.Size(8024)
      Me.lblFirst.TabIndex = 9
      Me.lblFirst.Text = "First Name"
      Me.lblFirst.TextAlign = System.Drawing.ContentAlignment.MiddleRight
      '
      'lblAddress
      '
      Me.lblAddress.Location = New System.Drawing.Point(10100)
      Me.lblAddress.Name = "lblAddress"
      Me.lblAddress.Size = New System.Drawing.Size(8024)
      Me.lblAddress.TabIndex = 10
      Me.lblAddress.Text = "Address"
      Me.lblAddress.TextAlign = System.Drawing.ContentAlignment.MiddleRight
      '
      'txtAddress
      '
      Me.txtAddress.Location = New System.Drawing.Point(100100)
      Me.txtAddress.Name = "txtAddress"
      Me.txtAddress.Size = New System.Drawing.Size(13620)
      Me.txtAddress.TabIndex = 2
      Me.txtAddress.Text = ""
      '
      'txtZip
      '
      Me.txtZip.Location = New System.Drawing.Point(100205)
      Me.txtZip.Name = "txtZip"
      Me.txtZip.Size = New System.Drawing.Size(13620)
      Me.txtZip.TabIndex = 5
      Me.txtZip.Text = ""
      '
      'txtCity
      '
      Me.txtCity.Location = New System.Drawing.Point(100135)
      Me.txtCity.Name = "txtCity"
      Me.txtCity.Size = New System.Drawing.Size(13620)
      Me.txtCity.TabIndex = 3
      Me.txtCity.Text = ""
      '
      'lblCity
      '
      Me.lblCity.Location = New System.Drawing.Point(10135)
      Me.lblCity.Name = "lblCity"
      Me.lblCity.Size = New System.Drawing.Size(8024)
      Me.lblCity.TabIndex = 11
      Me.lblCity.Text = "City"
      Me.lblCity.TextAlign = System.Drawing.ContentAlignment.MiddleRight
      '
      'lblState
      '
      Me.lblState.Location = New System.Drawing.Point(10170)
      Me.lblState.Name = "lblState"
      Me.lblState.Size = New System.Drawing.Size(8024)
      Me.lblState.TabIndex = 12
      Me.lblState.Text = "State"
      Me.lblState.TextAlign = System.Drawing.ContentAlignment.MiddleRight
      '
      'txtPhone
      '
      Me.txtPhone.Location = New System.Drawing.Point(100240)
      Me.txtPhone.Name = "txtPhone"
      Me.txtPhone.Size = New System.Drawing.Size(13620)
      Me.txtPhone.TabIndex = 6
      Me.txtPhone.Text = ""
      '
      'cmdOK
      '
      Me.cmdOK.Location = New System.Drawing.Point(112296)
      Me.cmdOK.Name = "cmdOK"
      Me.cmdOK.TabIndex = 15
      Me.cmdOK.Text = "OK"
      '
      'FrmValid
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(513)
      Me.ClientSize = New System.Drawing.Size(312341)
      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.EventArgsHandles 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
14.24.TextBox Validation
14.24.1.TextBox validating eventTextBox validating event
14.24.2.TextField validation: cannot be emptyTextField validation: cannot be empty
14.24.3.Validate user information using regular expressions.Validate user information using regular expressions.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.