Restrict string input-Regular Expression (VB. NET), regular expression vb.net
1. Overview
When a Data room charging system is built, almost all forms have text boxes or combos. When you perform operations, you must first determine whether the system is empty, then, you can determine the attributes. For example, you can only enter numbers for card numbers, student numbers, and amounts. For some information, you can only enter letters, or set some information to only numbers and letters. For example, when registering an email address, you can set some specific characters:
The following is the page for 163 email registration.
When we use the standard method to directly set up the data center charging system using ASCII code, we feel a little complicated, now let's get to know the regular expression, one of the seven skills required by programmers ".
2. What is a regular expression?
First, read a cartoon:
A regular expression is a regular expression that uses predefined characters and combinations of these characters to form a "rule string". This "rule string" is used to express a filtering logic for strings.
For example, you can only enter a number: "^ [0-9] * $ ".
Only Chinese characters can be entered: "^ [\ u4e00-\ u9fa5] {0,} $ ".
You can only enter A string consisting of 26 English letters: "^ [A-Za-z] + $ ".
3. How to use a regular expression?
Let's take a look at how to use it.
Let's take the form of adding a new user as an example:
Public Const C_REGULAR_LenNum = "^ [A-Za-z0-9] + $" 'only the Public Const C_REGULAR_NumLimit = "^ \ d {6, 16} $"' can contain numbers and 26 letters "' you can only enter 6 to 16 digits.
''' <Summary> ''' checks whether the input string matches the regular expression item''' </summary> ''' <param name = "strPattern"> Regular Expression items</ param> ''' <param name = "texregulatory"> input string </param> ''' <returns> whether the returned results match </returns> ''' <remarks> <span style = "font-family: kaiTi_GB2312; "> August 28, 2014 16:02:20 </span> </remarks> Public Function funCheckRegular (ByVal strPattern As String, texregulatory As String) Return Regex. isMatch (texregulatory, strPattern) End Function ''' <summar Y> ''' specific judgment function, determine whether to enter the number ''' </summary> ''' <param name = "arrayControl"> struct array </param> ''' <returns> True indicates a match, false does not match </returns> ''' <remarks> 10:30:24 </remarks> Public Function CheckRegular (ByVal arrayControl () As Term, ByVal strPattern As String, Warning As String) as Boolean Dim termControl As Term 'declares a Term type variable termcontrol' to traverse all elements In the struct array. If the control text input is invalid, the corresponding return value For Each termControl In ar is given. RayControl 'traverses all elements in the struct array If TypeOf termControl. controlSub Is TextBox Then 'to determine whether the control Is a text box If termControl. controlSub. text. trim <> "" then': determines whether the content of the text box is empty. If funCheckRegular (strPattern, termControl. controlSub. text) = False Then MessageBox. show (termControl. strText + "Bar --! "+ Warning," Reminder ", MessageBoxButtons. OK, MessageBoxIcon. information) termControl. controlSub. the 'focus () 'control is empty, and the Focus Return False Exit Function End If Next Return True End Function is obtained.
The following code calls the interface layer (UI:
Public Sub RdimStrNum () ReDim Preserve arrayControl (0) 'redefine array dimension 'initialize array arrayControl (0) = New Term (txtUserID, "username") End Sub Public Sub RdimNum () reDim Preserve arrayControl (1) 'redefines the array dimension 'initialize array arrayControl (0) = New Term (txtUserPWD, "password") arrayControl (1) = New Term (txtConfirm, "Confirm Password") End Sub
'Determine whether the user input is legal (numbers and letters) Call RdimStrNum () dim strPattern = C_REGULAR_LenNum 'regular expression Dim Warning As String = "only characters consisting of numbers and 26 English letters can be entered" If CheckRegular (arrayControl, strPattern, Warning) = False Then Exit Sub End If 'determines whether the user input is legal (number) Call RdimNum () dim pattern = C_REGULAR_NumLimit 'regular expression Dim Warn As String = "Please Input 6 to 16 digits" If CheckRegular (arrayControl, pattern, Warn) = False Then Exit Sub End If
3. Conclusion
In the daily work of programmers, data processing accounts for a considerable proportion. Text in all data accounts for a considerable proportion. Text can be understood and transparent, and is conducive to system development, testing, and maintenance. Regular Expressions are exactly what we need, it provides us with powerful string processing capabilities.
Reprinted please indicate the source: http://blog.csdn.net/zlts000/article/details/38021335
Regular Expression that limits the number of input strings
<Script>
Var a =/^. {1, 18} $ /;
Alert (a. test ("12d #"));
</Script>
How to restrict the use of special characters in a text box using regular expressions in the C # + winform form?
[^ % & ',; =? $ \ X22] +
This is matching 1 ~ N ^ % & ',; =? Characters other than $,
If f' is input, f' is input. If f matches f, true is returned.
The correct method is to add the start and end signs.
String putter = "^ [^ % & ',; =? $ \ X22] + $ "; indicates that the value ranges from the beginning to the end ~ N non-special characters