Create your own patter with regular expression in Visual Basic.Net. In order to create a regular expression pattern, first declared Imports System.Text.RegularExpressions above public class form. Regular expression is complicated to put a pattern because of the symbol declaration. however, I already created a simple pattern that restrict the input of a user. There’s should be satisfied the input otherwise error occurred.
1. Example pattern ^\d{5}$ that accepts 5 numeric digits.
Dim MyRegex As New Regex("^\d{5}$")Dim Input As String = TextBox2.TextIf MyRegex.IsMatch(Input) ThenMessageBox.Show("Correct")ElseMessageBox.Show("Error")End If
2. Example pattern ^(\d{3})(-\d{4})?$ that accepts 3 numeric digits, or 3 digits-dash-4 digits.
Dim MyRegex As New Regex("^(\d{3})(-\d{4})?$")Dim Input As String = TextBox2.TextIf MyRegex.IsMatch(Input) ThenMessageBox.Show("Correct")ElseMessageBox.Show("Error")End If
The above code matched any digits exactly 3 times, or you can specified [2-9] instead of \d, it’s up on how you are going to pattern. You can also create email address pattern based the above code, instead of dash(-) put At(@),Dot(.), etc. More about how to handle input data here and for Regular Expression here.
0 comments:
Post a Comment