When we make websites, especially in various e-commerce websites, we will first allow users to fill in some forms to obtain various information for registered users, because users may enter a wide variety of information, while some do not meet the requirements Will bring unnecessary trouble to our backend ASP handlers, or even cause some security issues. Therefore, before we save this information to the database database, we must perform the legitory check of the information entered by these users so that the following programs can be implemented safely. So we generally write an ASP check program to analyze whether the data entered by the user is legal. Perhaps someone will ask, do you use JavaScript running on the client to check the user's data more quickly? Indeed, in most cases, why is it in most cases? Because the JavaScript you wrote is not necessarily completely normal, running on IE and Netscape, because Microsoft's JScript is not full of JavaScript, plus some browsers do not necessarily and Netscape are very good, so very It is possible that the client's JavaScript does not accurately check the various data entered by the user, and the ASP program is running on the server, just related to your server, no matter what the client is browser, for your ASP program It is not separate, so selecting the check of data legitimacy using the backend ASP program is a good choice. When using the ASP to perform the legitory check of the backend, some people have written a lot of functions to meet the data valid below the different environments, for example, we want to verify that the URL address entered by the user is legal, it is You can write a piece of code to analyze the information entered by one by one by one character. The amount of information to be analyzed is small. It is also better to do. If it is the analysis of the conditions, it can be married, not only to write a very cumbersome Code, and the efficiency of run is extremely low, is there a good solution? Yes, that is, the "regular expression" object provided by Vbscritp5.0, as long as your server is installed IE5.x, it will take VBScript5.0. In fact, "Regular Expression" is originally the patent below Unix, especially in the Perl language, it is because of the powerful function of "regular expression", so that Microsoft slowly transplant the regular expression object to the window. The system is on the system, using the "Regular Expression" object, we can very convenient to verify the validity of various data. First, let's take a look at what is the "regular expression" object of VBScript, let's see a program:
Function ChecKexp (PATRN, STRNG) DIM Regex, Match 'establishes variables. Set regex = new regexp 'establishes regular expressions. Regex.pattern = PATRN 'Setting mode. Regex.ignoreCase = true 'Set whether you distinguish between characters. Regex.global = true 'Sets global availability. Matches = regex.test (strng) 'Executes the search. Checkexp = matches end function
In this program program, we see that you can use "new regexp" to get a regular expression object, then the assignment of the regular matching template, that is, tell the regular expression object, what do you want to match? Template, then use method test to detect whether the data to be processed does not match if the template we gives, if it does not match, it indicates that the data to be processed is not a legitimate data, which also implements the legitimacy of data. Test, we can see that using a design reasonable match template, we can easily check data information similar to a batch of formats. Of course, "Regular Expression" objects in VBScript5.0 also have many other methods and properties, such as method replace (), using him, we can quickly realize the online very fashionable UBB style forum and BBS, This is not within the scope of our discussion, will then discuss later, we now look at the methods and properties commonly used in data checks, common methods: Execute method description: Perform regular expressions for the specified string . Syntax: Object.execute (String) The syntax of the Execute method includes the following parts: Object: must be required. Always a name of a regexp object. String: Required. To perform a text string of the regular expression. Description: The design mode of regular expression search is set by the Pattern of the Regexp object. The Execute method returns a Matches collection, which contains each matching Match object found in the String. If matching is not found, Execute will return an empty Matches collection. Test method Description: Perform a regular expression search for the specified string and returns a boolean value indicating whether a matching mode is found. Syntax: Object.test (String) Test method syntax includes the following sections: Object: Required. Always a name of a regexp object. String: Required. To perform a text string search for a regular expression. Note: The actual mode of regular expression search is set by the Pattern property of the Regexp object. Regexp.global properties have no effect on the Test method. If a matching mode is found, the Test method returns true; otherwise returns false. Common Properties: Global Properties Description: Set or return a Boolean value indicating that the entire search string is full match or only match the first one. Syntax: Object.global [= true | false] Object parameter always regexp object. If the search is applied to the entire string, the value of the Global property is true, otherwise its value is false. The default setting is True. IgnoreCase property Description: Set or return a boolean value, indicating whether the mode search is case sensitive. Syntax: Object.IgnoreCase [= true | false] Object parameter is always a regexp object. If the search is case sensitive, the ignorecase property is false; otherwise true. The default is TRUE. Pattern property Description: Set or return the searched regular expression mode. This is the most important attribute, which we mainly set this property to implement data verification. Syntax: Object.pattern [= "SearchString"] The syntax of the Pattern property contains the following sections: Object: Required.
Always a regexp object variable. SearchString: Optional. The searched regular string expression. It may contain various regular expressions characters in the section table. Setting: Special characters and sequences are used when writing regular expressions. The following table describes the characters and sequences that can be used, and examples are given. Character Description: /: Tag the next character as a special character or a literal value. For example, "N" matches the character "n". "/ n" matches the laundry. Sequence "//" with "/" match, "/ (" match ". ^: Match the input start position. $: Match the end of the input. *: Match the previous character zero or several times. For example, "ZO *" can match "z", "zoo". : Match the previous character once or more. For example, "ZO " can match "ZOO", but do not match "z".?: Match the previous character zero Second or once. For example, "a? Ve?" Can match "ve" in "Never".: Match any character other than changing line characters. (Pattern) matches the pattern and remember match. Match the subtronomator Use Item [0] ... [N] from the matches collection as a result. If you want to match parentheses characters (and), "/ (" or "/)" can be used. X | Y: Match X or Y. For example, "z | food" matchs "z" or "food" match "ZOO" or "Food". {N}: n is a non-negative integer. Matching just n times. For example, "O {2}" cannot match "O" in BOB, but can match the first two o "fooOOD". {N,}: n is a non-negative integer. Match at least n times. For example, "O {2,}" does not match "O" in "Bob", but matches all O. "O {1,}" in "fooOOD" is equivalent to "o ". "O {0,}" Equivalent to "o *". {N, m}: m and n are non-negative integers. Match at least n times, up to M times. For example, "O {1, 3}" matches the top three in "foooood" o. "o {0,1}" is equivalent to "o?". [XYZ]: A character set. Match one of the characters in parentheses. For example, "[ABC]" matches "plain" a ". [^ xyz]: A negative character set. Matching any character in this parentheses. For example," [^ ABC] "can match" P "in" PLAIN ". [AZ]: Represents a range The inner character. Match any character in the specified interval. For example, "[AZ]" matches any lower case character between "A" and "Z". [^ Mz]: Negate character interval. Always specified Characters in the interval match. For example, "[MZ]" matches any character that is not in "M" to "Z". / B: Match the boundary of the word, that is, the position between words and spaces.
For example, "ER / B" matches "ER" in "Never", but does not match "ER" in "Verb". / B: Match with non-word boundary. "EA * R / B" matches "EAR" in "NEVER EARLY". / d: Match with a numeric character. Equivalent to [0-9]. / D: Match with non-digital characters. Equivalent to [^ 0-9]. / f: Match with the pacharge. / N: Match with the wrap character. / R: Match with the carriage return character. / S: Match with any white character, including spaces, tabs, pagins, etc. Equivalent "[/ f / n / r / t / v]". / S: Match with any non-blank character. Equivalent to "[^ / f / N / R / T / V]". / T: Matching the player. / V: match the vertical tab. / W: Match with any word character, including underscore. Equivalent to "[A-ZA-Z0-9_]". / W: Match with any non-word character. Equivalent to "[^ a-za-z0-9_]". / NUM: Match NUM, where NUM is a positive integer. Quote Back to remember the match. For example, "(.) / 1" matches two consecutive identical characters. / N: Match N, where N is an octaves. The octal modulation value must be 1, 2 or 3 numbers long. For example, "/ 11" and "/ 011" matches a tab. "/ 0011" equivalent to "/ 001" and "1". The octal exchange value must not exceed 256. Otherwise, only the first two characters are considered part of the expression. Allows the use of the ASCII code in the regular expression. / xn: Match N, where N is a hexadecimal modified value. The hexadecimal modified value must be long for two numbers. For example, "/ x41" matches "A". "/ x041" equivalent to "/ x04" and "1". Allows the use of the ASCII code in the regular expression.
Ok, common methods and attributes are these, the above syntax introduction has been very detailed, we don't have to be in Luo, let's take a look at how to use these methods and attributes in specific examples. We are still an example. For example, we want to check the email entered by the user, then what kind of data is a legal email? I can type this: uestc95@263.net, of course I will also enter: xxx@yyy.com.cn, but such input is illegal: xxx @@ com.cn or @ xxx.com.cn, etc. Therefore, we have a legal email address at least to meet the following conditions: 1. You must contain one and only one symbol "@" 2. must contain at least one of the three symbols "." 3. First character Do not be "@" or "." 4. Do not allow "@." Or. @ 5. The end is not allowed to be character "@" or "." So according to the above principles and the syntax in the above table, we are easy. You can get the needs of the template as follows: "(/ w) [@] {1} (/ w) [.] {1, 3} (/ w) " Next, we carefully analyze this template, first " / w "indicates that the start character of the message can only be a word character containing underscore, which satisfies the third condition;" [@] {1} "indicates that the email should match and only match the character" @ " , Satisfying the conditional one; the same "[.] {1, 3}" indicates that at least one matched 3 characters in the email ".", Satisfying the second condition; template last "(/ W ) "The character that can only be the word character included in the underscore, satisfying the conditional five; (/ W) " in the middle of the template satisfies the condition four. Then, we directly call the function checkexp ("(/ w) [@] {1} (/ w) [.] {1} (/ w) ", the string to be verified) Ok, if it returns true, it means that the data is legal, otherwise it is incorrect, how, simple. We can also write a template for checking ID card: "([0-9]) {15}"; Template for verifying the URL: "^ http: // {1} (/ w) [.] ) {1, 3} ", etc. We can see that these templates provide us with a very heavyweight module, using the various templates provided by others or others, we can make it easy and fast data. Sexual verification, I believe you will definitely write a very common template. This way, we can customize the legality check of different data as long as we customize different templates. Therefore, the most important attributes in the regular expression object are: "pattern" attribute, as long as this attribute is truly, it can be freely use the regular expression object to serve our data validation. Author:
Su Hongchao