When creating websites, especially various e-commerce websites, we first ask users to fill in some forms to obtain various information about registered users, because users may enter various types of information, some non-conforming data may cause unnecessary troubles to Our backend ASP processing program, and even cause some security problems to the website. Therefore, before saving the information to the database of the website, we need to verify the validity of the information entered by these users so that subsequent programs can be executed safely and smoothly. Therefore, we usually write an ASP validation program on the backend to analyze whether the user input data is valid.
Someone may ask, isn't JavaScript running on the client better and faster to verify user data? Indeed, this is acceptable in most cases. Why? Because the JavaScript you write may not run completely normally on IE and Netscape at the same time, because Microsoft's Jscript is not the same as JavaScript, in addition, some browsers are not necessarily compatible with Microsoft and Netscape, so it is very likely that Javascript on the client will not accurately verify the various types of user input data, while ASP programs are running on the server, it is only related to the environment of your server. No matter what browser the client is, there is no difference for your ASP program, therefore, it is a good choice to use the back-end ASP program for Data Validity verification.
When ASP is used for backend Data Validity verification, some people manually perform data verification in different environments and write a lot of functions. For example, to verify that the URL address entered by a user is valid, you can write a code to analyze the user input information one by one. The amount of information to be analyzed is small, if the analysis conditions are ever-changing, it will be miserable. Not only do you have to write a very long and tedious code, but the running efficiency is extremely low? Is there a good solution? Yes, it is the "Regular Expression" object provided by VBScritp5.0. If your server has IE5.x installed, it will contain VBScript5.0. In fact, "regular expressions" were originally patents under Unix, especially the most widely used in Perl, because of the powerful functions of "regular expressions, so that Microsoft can port the regular expression object to the Windows system and use
For the "Regular Expression" object, we can easily verify the validity of various types of data.
First, let's take a look at what is VBScript's "Regular Expression" object. Let's first look at a program:
Function CheckExp (patrn, strng)
Dim regEx, Match 'to create a variable.
Set regEx = New RegExp 'to create a regular expression.
RegEx. Pattern = patrn 'setting mode.
RegEx. IgnoreCase = true' specifies whether the characters are case sensitive.
RegEx. Global = true' to set Global availability.
Matches = regEx. test (strng.
CheckExp = matches
End Function
In this program, we can see that we can use "New RegExp" to get a regular expression object, and then assign a value to this object to the regular expression template, that is, to tell the regular expression object, what kind of template Do You Want To match, and then use the method Test to check whether the data to be processed exactly matches the template we provide. If not, it indicates that the data to be processed is not legal, and thus the data validity is verified. We can see that a well-designed matching template is used, we can easily verify a batch of similar data information.
Of course, the "Regular Expression" Object in VBScript5.0 has many other methods and attributes, such as method Replace (), with him, we can quickly implement the very fashionable UBB-style forums and BBS on the Internet. This is beyond our scope of discussion and will be discussed later, now let's take a look at the common methods and attributes of Regular Expression objects in data validation:
Common method: Execute Method
Description: searches for a specified string using a regular expression.
Syntax: the syntax of the object. Execute (string) Execute method includes the following parts:
Object: required. It is always the name of a RegExp object.
String: required. The text string on which the regular expression is to be executed.
Note: The regular expression search design mode is set through the Pattern of the RegExp object. The Execute method returns
The Matches set contains each matching Match object found in the string. If no match is found, Execute returns an empty Matches set.
Test Method
Description: performs a regular expression search on the specified string and returns a Boolean value indicating whether a matching pattern is found.
Syntax: object. Test (string)
The syntax of the Test method includes the following parts:
Object: required. It is always the name of a RegExp object.
String: required. The text string to be searched by the regular expression.
Note: The actual Pattern of Regular Expression search is set through the Pattern attribute of the RegExp object. The RegExp. Global attribute has no effect on the Test method. If the matching mode is found, the Test method returns True; otherwise, False.
Common attributes: Global attributes
Description: sets or returns a Boolean value, which indicates whether the pattern matches all or only the first character in the entire search string.
Syntax: object. Global [= True | False]
The object parameter is always a RegExp object. If the search is applied to the entire string, the value of the Global attribute is True; otherwise, the value is False. The default value is True.
IgnoreCase attributes
Description: sets or returns a Boolean value to indicate whether the mode search is case sensitive.
Syntax: object. IgnoreCase [= True | False]
The object parameter is always a RegExp object. If the search is case sensitive, the IgnoreCase attribute is False; otherwise, the value is True. The default value is True.
Pattern attributes
Description: sets or returns the regular expression pattern to be searched. This is the most important attribute. We mainly set this attribute to implement data verification.
Syntax: object. Pattern [= "searchstring"]
The syntax of the Pattern attribute includes the following parts:
Object: required. Always a RegExp object variable.
Searchstring: Optional. The regular string expression to be searched. It may contain various regular expression characters in some tables.
Setting: special characters and sequences are used when writing regular expressions. The following table describes the characters and sequences that can be used and provides examples.
Character Description: \: Mark the next character as a special character or literal value. For example, "n" matches the character "n. "\ N" matches the linefeed. The sequence "\" matches with "\", and "\ (" matches.
^: Match the start position of the input.
$: Matches the end of the input.
*: Match the first character Zero or several times. For example, "zo *" can match "z" and "zoo ".
+: Match the previous character once or multiple times. For example, "zo +" can match "zoo" but does not match "z ".
? : Match the first character Zero or once. For example, "? Ve? "Matches" ve "in" never ".
.: Match any character other than the line break.
(Pattern) matches the pattern and remembers the matching. The matched substring can be obtained from the Matches set used as the result using Item [0]... [n. To match the parentheses (and), use "\ (" or "\)".
X | y: matches 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. Match exactly n times. For example, "o {2}" cannot match "o" in "Bob", but it can match the first two o in "foooood.
{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 "foooood. "O {1,}" is equivalent to "o + ". "O {0,}" is equivalent to "o *".
{N, m}: non-negative integers of m and n. Match at least n times, at most m times. For example, "o {1, 3}" matches the first three o in "fooooood. "O {0, 1}" is equivalent to "o? ".
[Xyz]: A character set. Matches one of the characters in the brackets. For example, "[abc]" matches "a" in "plain ".
[^ Xyz]: A negative character set. Match any character that does not exist in this bracket. For example, "[^ abc]" can match "p" in "plain ".
[A-z]: a character in a certain range. Matches any character in the specified range. For example, "[a-z]" matches any lowercase letter between "a" and "z.
[^ M-z]: A negative character range. Matches a character that is not in the specified range. For example, "[m-z]" matches any character that is not between "m" and "z.
\ B: match the boundary of a word, that is, the position between a word and a space. For example, "er \ B" matches "er" in "never", but does not match "er" in "verb ".
\ B: matches non-word boundary. "Ea * r \ B" matches "ear" in "never early.
\ D: matches a number character. It is equivalent to [0-9].
\ D: matches non-numeric characters. It is equivalent to [^ 0-9].
\ F: match with the paging character.
\ N: match the linefeed character.
\ R: matches the carriage return character.
\ S: matches with any white characters, including spaces, tabs, and pagination characters. It is equivalent to "[\ f \ n \ r \ t \ v]".
\ S: matches any non-blank characters. It is equivalent to "[^ \ f \ n \ r \ t \ v]".
\ T: matches the tab.
\ V: match the vertical tab.
\ W: matches any word characters, including underscores. It is equivalent to "[A-Za-z0-9 _]".
\ W: matches any non-word characters. It is equivalent to "[^ A-Za-z0-9 _]".
\ Num: matches num, where num is a positive integer. Reference back to the remembered match. For example, "(.) \ 1" matches two consecutive identical characters.
\ N: Match n, where n is an octal value. The octal value must be 1, 2, or 3 characters long.
For example, "\ 11" and "\ 011" Both match a tab. "\ 0011" is equivalent to "\ 001" and "1 ". The octal value cannot exceed 256. Otherwise, only the first two characters are considered part of the expression. ASCII code can be used in regular expressions.
\ Xn: matches n, where n is a hexadecimal value. The hexadecimal value must be exactly two digits long. For example, "\ x41" matches "". "\ X041" is equivalent to "\ x04" and "1 ". ASCII code can be used in regular expressions.
Well, these are the common methods and attributes. The above syntax has already been described in detail, so we don't have to go on to it, next, let's take a look at how to use these methods and attributes to verify the validity of data in specific examples. Let's take an example. For example, we want to verify the email entered by users, so what kind of data is a legal email? I can enter: 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, and so on, therefore, a valid email address must meet at least the following conditions:
1. It must contain one and only one symbol "@".
2. It must contain at least one of the three symbols "."
3. The first character cannot be "@" or "."
4. "@." Or ". @" cannot appear .@
5. The end cannot be the character "@" or "."
Therefore, based on the above principles and the syntax in the above table, we can easily obtain the required template as follows: "(\ w) + [@] {1} (\ w) + [.] {1, 3} (\ w) +"
Next, we will analyze this template carefully. "\ w" indicates that the start character of an email can only be a word character that contains an underscore. This satisfies the third condition; "[@] {1}" indicates that the email address must match only one character "@", meeting condition 1. The same "[.] {1, 3} "indicates that at least one character matches up to three characters in the email". the second condition is met. The final "(\ w) +" of the template indicates that the ending character can only be a word character including an underscore, satisfying condition 5; the "(\ w) +" in the template meets Condition 4.
Then, we can directly call the previous function CheckExp ("(\ w) + [@] {1} (\ w) + [.] {1} (\ w) + ", string to be verified). If True is returned, the data is valid. Otherwise, the data is incorrect. How can this problem be solved. We can also write a template for verifying the ID card number: "([0-9]) {15}"; the template for verifying the URL: "^ http: // {1} (\ w) + [.]) {1, 3} ", etc. We can see that these templates provide us with good Reusable Modules and use various templates provided by ourselves or others, we can check the validity of data conveniently and quickly. I believe you will write a very general template.
In this way, we only need to customize different templates to verify the legitimacy of different data. Therefore, the most important attribute of a regular expression object is the "Pattern" attribute. As long as you have mastered this attribute, you can use the regular expression object for data verification.