Restrict string input--regular form (vb.net)

Source: Internet
Author: User

1. Overview

in the computer room charging system, almost all of the window has a text box or combo box, when the user to operate. The first is to infer whether it is empty, and then infer the various properties, such as card number, school number, amount, etc. can only enter a number, some information can only enter letters, or some information input can be set to only enter numbers and letters. Give me a sample example. When we register our e-mail. There will be some set of specific characters:

The following is a page of 163 email Brochure

When we do the computer room charge system. Given the usual method of setting up the ASCII code directly, it feels a little complicated, and now we are together to recognize one of the seven skills necessary to recognize a program ape.



2, what is the form of the formula?

First Look at a comic:


a regular form is a "rule string" that is used to express a filter logic for a string by using a predefined set of specific characters, and a combination of these specific characters.

For example: You can only enter a number: "^[0-9]*$".

Can only input Chinese characters: "^[\u4e00-\u9fa5]{0,}$".


You can only enter a string consisting of 26 English letters: "^[a-za-z]+$".



3, how to use the form of the formula?

Here's a look at how to use it.

Let's take the example of adding a new user window:


    Public Const C_REGULAR_LENNUM = "^[a-za-z0-9]+$"         can only enter a character that consists of a number and 26 English letters public    const C_REGULAR_NUMLIMIT = "^\\ d{6,16}$ "          ' can only enter numbers from 6 to 16 digits.

    "<summary>" detects if the input string matches the normal table "" ' </summary> ' "<param name=" Strpattern "> the regular table-of-entry </ Param> ' <param name= ' textreg ' > Input string </param> ' <returns> return result matches </returns> ' ' &lt ; Remarks><span style= "font-family:kaiti_gb2312;" >2014 August 28 16:02:20</span></remarks> public Function funcheckregular (ByVal Strpattern as String, Textreg as String) Return Regex.IsMatch (Textreg, Strpattern) End function ' <summary> ' ' run in a detailed inference letter Number, infer whether to enter the number ' </summary> ' ' <param name= ' arraycontrol ' > struct array </param> ' <returns>true Indicates a match, false does not match </returns> ' <remarks>2014-07-09 10:30:24</remarks> public Function checkregular (B                                      Yval Arraycontrol () as term, ByVal Strpattern as String, Warning as String) as a Boolean Dim Termcontrol as term ' Declare a term type variable Termcontrol ' to traverse all elements in the struct array. Assume that the control text input is not valid. The corresponding return is given.The value for every termcontrol in Arraycontrol ' iterates over all elements in the struct array If TypeOf termcontrol.contr Olsub is TextBox and then ' infers if the control is a text box if TermControl.controlSub.Text.Trim <> ' then ' infers the text box Whether the content is empty.                        Do not run down for null If funcheckregular (Strpattern, termControl.controlSub.Text) = False Then MessageBox.Show (Termcontrol.strtext + "Bar--!

+ Warning, "friendly Tips", MessageBoxButtons.OK, MessageBoxIcon.Information) termControl.controlSub.Focus () ' Give the focus to the empty control Return False Exit Function End If End If Next Return True End Function


The following code is called at 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)  ' redefine array dimension        ' Initialize array        arraycontrol (0) = new term (txtuserpwd, "password")        Arraycontrol (1) = new Term (txtconfirm, " Confirm Password ")    End Sub

        ' Infer whether the user input is legal (numbers and letters) call        Rdimstrnum ()        Dim Strpattern = C_regular_lennum        ' Regular table        Dim Warning As String = " Can only enter a character consisting of a number and 26 English letters "        if Checkregular (Arraycontrol, Strpattern, Warning) = False then            Exit Sub        End if        ' Infer if user input is legal (numeric)        Call Rdimnum ()        Dim pattern = C_regular_numlimit    ' Regular form        Dim Warn As String = "Please enter 6 to 16 digits"        If Checkregular (Arraycontrol, pattern, Warn) = False then            Exit Sub        End If


3. Concluding remarks

in the routine of the ape-work. The data processing occupied a considerable proportion. and the entire data in the text has occupied a considerable proportion. Text can be understood by people, with good transparency, conducive to system development, testing and maintenance must have a certain regularity to follow a rule. And that is exactly what we need, and it gives us the ability to deal with strings in a powerful form.





Reprint Please specify source:http://blog.csdn.net/zlts000/article/details/38021335


Restrict string input--regular form (vb.net)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.