Use "Regular Expression Object" in ASP to verify the legality of data

zhaozj2021-02-11  163

Use "Regular Expression Object" in ASP to verify the legality of data

Keywords: ASP

In ASP using "regular expression object" to verify the legitimacy of data (Author: Suhongtu Super 2000 at 13:35 on March 27)

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 above, using

"Regular Expression" object, we can very convenient verification of all kinds of 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 method and attributes commonly used in the data checks in the data check:

Common method: Execute method

Description: Perform regular expressions search for the specified string.

Syntax: Object.execute (String) The syntax of the Execute method includes the following parts:

Object: must be. 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. EXECUTE method returns one

Matches collection 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.

Grammar: Object.test (String)

The syntax of the Test method includes the following parts:

Object: must be. 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 property

Description: Set or return a boolean value, which indicates that the entire search string is full match or only the first one.

Syntax: Object.global [= true | false]

Object parameters always regexp objects. 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.

Grammar: Object.Ignorecase [= true | false]

The 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 parts: Object: must be. 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, "/ (" matches ".

^: Match the start position of the input.

$: 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 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 to match. Matching subtrips can be obtained from Item [0] ... [N] from the matches collection as a result. If you want to match parentheses characters (and), you can use "/ (" or "/)."

X | Y: Match X or Y. For example, "z | food" can match "Z" or "Food". "(z | f) OOD" matches "ZOO" or "Food".

{n}: n is a non-negative integer. The match is 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 in "fooOOD". "o {1,}" is equivalent to "o ". "o {0,}" is 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 O "foooood". "o {0,1}" is equivalent to "o?".

[XYZ]: A character set. Match one of the characters in parentheses. For example, "[ABC]" matches "A" in "Plain".

[^ XYZ]: A negative character set. Match any character in this parentheses. For example, "[^ ABC]" can match "P" in "PLAIN".

[A-Z]: Represents characters within a range. Match with any character in the specified interval. For example, "[A-Z]" matches "A" and "Z" any lowercase letters characters.

[^ m-z]: Negate character interval. Match with characters that are not within the specified interval. For example, "[m-z]" matches any character between "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. Must contain one and only one symbol "@"

2. Must include at least one of the three symbols "."

3. The first character must 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 can easily get the needs of the templates 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 that contains underscore, which satisfies the third condition;" [@] {1} " Indicates that the email should be matched and can only match the character "@", which satisfies the conditional one; the same "[.] {1, 3}" indicates that at least one matching 3 characters in the email. " ", Satisfying the second condition; the final" (/ w) "of the template indicates that the characters ending can only be included in the underscore, satisfying the conditional five; (/ w)" (/ w) "satisfaction Conditions 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.

Use "Regular Expression Object" in ASP to verify the legality of data

Keywords: ASP

In ASP using "regular expression object" to verify the legitimacy of data (Author: Suhongtu Super 2000 at 13:35 on March 27)

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 method and attributes commonly used in the data checks in the data check:

Common method: Execute method

Description: Perform regular expressions search for the specified string.

Syntax: Object.execute (String) The syntax of the Execute method includes the following parts:

Object: must be. 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. EXECUTE method returns one

Matches collection 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. Grammar: Object.test (String)

The syntax of the Test method includes the following parts:

Object: must be. 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 property

Description: Set or return a boolean value, which indicates that the entire search string is full match or only the first one.

Syntax: Object.global [= true | false]

Object parameters always regexp objects. 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.

Grammar: Object.Ignorecase [= true | false]

The 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 parts:

Object: must be. 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, "/ (" matches ".

^: Match the start position of the input.

$: 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 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 to match. Matching subtrips can be obtained from Item [0] ... [N] from the matches collection as a result. If you want to match bracket characters (and)

转载请注明原文地址:https://www.9cbs.com/read-5177.html

New Post(0)