Validate data and dates using Regular Expressions in .NET
Regular expressions provide a flexible and efficient way to process text. The
extensive pattern-matching notation of regular expressions lets you to quickly
parse large amounts of text to find specific character patterns and to extract,
edit, replace, or delete text substrings. Here I show how to match a basic
pattern.
Basically, you can use regular expressions in a manner similar to the
LIKE operator. The regular expression language consists of 2 types
of elements: literals and metacharacters. It's the
metacharacters that provide the power. In the simplest case metacharacters are
like wild cards. For example: "Copy *.DOC". The "*" matches zero or more
occurrences of any character. It is definately worth reading about regular
expressions in MSDN.
.NET makes regular expression functionality available through the
System.Text.RegularExpressions namespace. This example uses the
Regex.IsMatch method.
Say your application allows for the input of a positive or negative decimal
value who's format must not exceed 3 digits to the left of the decimal point
and 2 digits to the right (precision of 2). A minus sign ("-") may be input but
a plus sign ("+") may not. Verifying the input value is numeric is easy with
the IsNumeric command but verifying its exact
syntax is not. Regular expressions make this possible.
Table 1
|
|
Table 2
|
Expression
|
Valid Values
|
|
Invalid Values
|
1
|
0.
|
|
a
|
1
|
111.
|
|
+1
|
2
|
0
|
|
.
|
2
|
111
|
|
.001
|
3
|
0.0
|
|
0.0.
|
3
|
0.01
|
|
1.111
|
3
|
.01
|
|
1111
|
3
|
1.1
|
|
-.
|
3
|
111.1
|
|
+0
|
3
|
+111.11
|
|
1111.11
|
This code in the TextChanged event for the input textbox displays the
ErrorProvider icon when an invalid value is entered from the
keyboard or pasted in.
Imports System.Text.RegularExpressions
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Const REGULAR_EXP = "(^-?\d{1,3}\.$)|(^-?\d{1,3}$)|(^-?\d{0,3}\.\d{1,2}$)"
'
' Clear the error provider icon.
'
ErrorProvider.SetError(TextBox1, "")
Dim aValue As String = TextBox1.Text.Trim
'
' Only check if a value exists since a value is optional.
'
If aValue <> "" Then
'
' Check its syntax.
'
If Not Regex.IsMatch(aValue, REGULAR_EXP) Then
ErrorProvider.SetError(TextBox1, Invalid Value")
TextBox1.Focus
End If
End If
End Sub
The "(^-?\d{1,3}\.$)|(^-?\d{1,3}$)|(^-?\d{0,3}\.\d{1,2}$)" statement is really
three expressions OR-ed together with the
"|" operator. Each expression is grouped
using the parentheses "()". Grouping says to look at the contents of the group
as a whole before performing the OR.
The first 2 expressions handle the portion of the input value to the left of the
decimal as shown in Table 1. Expression 1 (^-?\d{1,3}\.$) matches numeric
values ("\d") with a minimum length of 1 and a maximum length of 3 characters
("{1,3}") with an optional minus sign ("-?" - the question mark means "0 or 1
time"). The sequence of characters MUST be followed by a decimal point ("\." -
the backslash is an escape character meaning do not interpret the following
characher i.e. use it as a literal).
Expression 2 (^-?\d{1,3}$) matches numeric values ("\d") 1 to 3 characters long
("{1,3}") with an optional minus sign ("-?") which are NOT followed by a
decimal point.
Expression 3 (^-?\d{0,3}\.\d{1,2}$) matches numeric values ("\d") 0 to 3
characters long ("{0,3}") with an optional minus sign ("-?") followed by a
decimal point and 1 or 2 numeric characters ("\d") to the right of the decimal
("{1,2}").
Taken together these regular expressions will accept all legal values from
Table 1 and reject all values that do not match the pattern (Table 2).
You can check a date in "yyyy-MM-d" format using the following code. This
regular expression insures the date is in the desired format but doesn't check
for invalid dates such as "2003-01-34". That is why IsDate
is used.
Imports System.Text.RegularExpressions
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Const REGULAR_EXP = "(^[0-9]{4,4}-?[0-1][0-9]-?[0-3][0-9]$)"
. . .
If (Not Regex.IsMatch(aValue, REGULAR_EXP_DATE_STD)) Or _
(Not IsDate(aValue)) Then
ErrorProvider.SetError(TextBox1, Invalid Value")
TextBox1.Focus
End If
. . .
End Sub
You can also check a date that is more flexible in format. The date must be
entered as "mm/dd/yyyy". However, it can have a one or two digit month, a one
or two digit day and the separator can be a slash, dash or period. Further,
both separators do not need to be the same. Example, "1/2/2006", "01-02-2006",
"1/02-2006" are all valid as is "1.2.2006"
The following code uses the Matches method to break
the date into a its component parts. Each component or group is named using the
(?<>) syntax. The Matches method stores the groups in a
MatchCollection from which we can retrieve the names groups and
reformat the date to be in "mm/dd/yyyy" format.
Const REGEX_DATE As String = _
"^(?0[1-9]|[1-9]|1[012])(?[- /.])(?0[1-9]|[1-9]|[12][0-9]|3[01])(?[- /.])(?(19|20)\d\d)"
Dim mc As RegularExpressions.MatchCollection
mc = RegularExpressions.Regex.Matches("1-2-2006", REGEX_DATE)
If mc.Count > 0 Then
If mc.Item(0).Groups.Count >= 7 Then
Dim strMonth As String = mc.Item(0).Groups("Month").Value.PadLeft(2, "0"c)
Dim strDay As String = mc.Item(0).Groups("Day").Value.PadLeft(2, "0"c)
Dim strYear As String = mc.Item(0).Groups("Year").Value
'Format the date as 01/02/2006
Dim strDateNew As String = strMonth & "/" & strDay & "/" & strYear
End If
End If
|