When we make a website, especially a variety of e-commerce sites, first of all will let users fill out a number of forms to obtain registered users of various information, because users can enter a wide range of information, Some of the data does not meet the requirements of our backend ASP processing program to bring unnecessary trouble, and even caused some security problems on the site. Therefore, before we save this information to the database of the website, we should validate the data of the information entered by these users so that the subsequent programs can be executed safely and smoothly. So we typically write an ASP's validator at the back end to analyze whether the data entered by the user is legitimate.
Perhaps someone might ask, is it possible to use JavaScript that runs on the client to verify the user's data better and faster? Indeed, this is possible in most cases, why is it most of the case? Because the javascript you write doesn't necessarily run on IE and Netscape at the same time as Microsoft's JScript is not entirely the same as JavaScript, plus some browsers are not necessarily compatible with Microsoft and Netscape, So it is likely that the client's JavaScript will not accurately verify the user input of various data, and the ASP program is running on the server side, but with your server environment, regardless of what the client browser, for your ASP program is no difference, Therefore, it is a good choice to use the backend ASP program to validate the data legality.
When using ASP to verify the validity of the data in the backend, some people to meet the different environment under the data validation, wrote a lot of functions to achieve, for example, we want to verify the user input URL address is legitimate, you can write a piece of code to individual character analysis of the user input information, To analyze the amount of information is small, it is better to do, if the analysis of the changing conditions, that can be miserable, not only to write a very long and cumbersome code, and the efficiency of the operation is extremely low, there is no good solution? Yes, that's the "Regular expression" object provided by VBScritp5.0, as long as your server has ie5.x installed, it will bring VBScript5.0. In fact, the "regular expression" is originally a patent under Unix, especially in the Perl language is the most widely used, it is because of "regular expression" powerful features, so that Microsoft slowly transfer the regular expression object to the Windows system, the use of
Regular Expression object, we can easily validate the validity of various data.
First, let's take a look at what the "regular expression" object is in VBScript, let's start with a procedure:
Function checkexp (PATRN, STRNG)
Dim regEx, Match ' establishes the variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether character case is case-sensitive.
Regex.global = True ' Sets global availability.
matches = regex.test (strng) ' performs the search.
Checkexp = Matches
End Function
In this program, we see that we can use "New RegExp" to get a regular expression object, and then make a regular matching template assignment to that object, which tells the regular expression object what kind of template you want to match. Then use method test to detect whether the data to be processed and we give the template is matched, if not match, it indicates that the data to be processed is not legitimate data, thus achieving the validation of data legality, we can see that, using a reasonable design matching template, We can easily verify a batch of similar format data information.
Of course, the regular expression object in VBScript5.0 has many other methods and properties, such as method replace (), so that we can quickly realize the current online very fashionable UBB style forum and BBS, which is not within the scope of our discussion, to be discussed later, Let's take a look at it. Methods and properties commonly used in data validation for regular expression objects:
Common method: Execute method
Description: Performs a regular-expression search on the specified string.
Syntax: Object. The syntax of the Execute (String) Execute method includes the following sections:
Object: Required. Always the name of a RegExp object.
String: Required. The text string on which to execute the regular expression.
Description: The design pattern of regular expression search is set by RegExp object patterns. The Execute method returns a
The Matches collection, which contains each matching match object found in string. If no match is found, Execute returns an empty matches collection.
Test method
Description: Performs a regular expression search on the specified string and returns a Boolean indicating whether the matching pattern is found.
Syntax: Object. Test (String)
The syntax of the Test method includes the following sections:
Object: Required. Always the name of a RegExp object.
String: Required. The text string to perform a regular expression search for.
Description: The actual pattern of the regular expression search is set by the RegExp object's Patterns property. The Regexp.global property has no effect on the test method. The test method returns true if a matching pattern is found, otherwise it returns false.
Common Properties: Global properties
Description: Sets or returns a Boolean value that indicates whether the pattern matches all or only the first one throughout the search string.
Syntax: Object. Global [= True | False]
The object argument is always REGEXP objects. If the search applies 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: Sets or returns a Boolean value that indicates whether the pattern search is case-sensitive.
Syntax: Object. IgnoreCase [= True | False]
The object argument is always a RegExp object. If the search is case-sensitive, the IgnoreCase property is False; otherwise, true. The default value is True.
Pattern Properties
Description: Sets or returns the regular expression pattern that is searched. This is one of the most important attributes, and we mainly set this property to implement data validation.
Syntax: Object. pattern [= "searchstring"]
The syntax of the pattern attribute consists of the following sections:
Object: Required. Always a REGEXP object variable.
SearchString: Optional. The regular string expression being searched. It may contain various regular expression characters in the Set section table.
Settings: Special characters and sequences are used when writing patterns for regular expressions. The following table describes the characters and sequences that can be used, and gives an example.
Character description: \: Marks the next character as a special character or literal. For example, "n" matches the character "n". "\ n" matches the line feed character. The sequence "\" and "\" match, "\" ("Match").
^: matches the start position of the input.
$: Matches the end of the input.
*: Matches the previous character 0 times or several times. For example, "zo*" can Match "Z", "Zoo".
+: Matches the previous character one or more times. For example, "zo+" can Match "zoo", but does not match "Z".
? : matches the previous character 0 times or once. For example, "A?ve?" Can match "never" in the "VE".
.: matches any character other than the newline character.
(pattern) matches patterns and remembers matches. The matching substring can be used with the Item [0] from the matches collection as the result ... [n] obtained. If you want to match the bracket characters (and), you can 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. Matches exactly n times. For example, "o{2}" cannot match "O" in Bob, but 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}: M and n are non-negative integers. 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 parentheses. For example, "[ABC]" matches "a" in "plain".
[^XYZ]: a negative character set. Matches any character that is not in this bracket. For example, "[^ABC]" can match "P" in "plain".
[A-Z]: represents a range of characters. Matches any character within the specified interval. For example, "[A-z]" matches any of the lowercase alphabetic characters between "a" and "Z".
[^m-z]: a negative character range. Matches characters that are not in the specified interval. For example, "[M-z]" matches any character that is not between "M" and "Z".
\b: Matches the boundary of a word, which 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 a non word boundary. "ea*r\b" matches the "ear" in "Never early".
\d: Matches a numeric character. equivalent to [0-9].
\d: Matches non-numeric characters. equivalent to [^0-9].
\f: Matches page breaks.
\ n: matches the line break character.
\ r: Matches the carriage return character.
\s: Matches any white character, including spaces, tabs, page breaks, and so on. Equivalent to "[\f\n\r\t\v]".
\s: Matches any non-white-space character. Equivalent to "[^ \f\n\r\t\v]".
\ t: Matches tabs.
\v: Matches the Vertical tab.
\w: Matches any word characters, including underscores. Equivalent to "[a-za-z0-9_]".
\w: matches any non word character. Equivalent to "[^a-za-z0-9_]".
\num: Matches num, where num is a positive integer. References are returned to the remembered match. For example, "(.) \1 "matches two consecutive identical characters.
\ n: Match n, where n is a octal value. The octal change value must be 1, 2 or 3 digits long.
For example, both "\11" and "\011" match one tab. "\0011" is equivalent to "\001" and "1". The value of the octal converter cannot exceed 256. Otherwise, only the first two characters are considered part of an expression. Allows ASCII code to be used in regular expressions.
\xn: Matches n, where n is a hexadecimal code-changing value. The hexadecimal transposition value must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04" and "1". Allows ASCII code to be used in regular expressions.
Okay, commonly used methods and attributes are these, the above syntax is very detailed, we do not need to wordy, and then we look at the specific examples of how to use these methods and attributes to verify the legality of the data, we still give an example, for example, We want to verify the email that the user entered, so what kind of data is a legitimate email? I can enter this way: uestc95@263.net, of course I will enter this way: xxx@yyy.com.cn, but such input is illegal: xxx@ @com. cn or @xxx.com.cn, etc., so we come up with a legitimate email address that should meet at least several of the following conditions:
1. Must contain one and only one symbol "@"
2. Must contain at least one maximum of three symbols "."
3. The first character must not be "@" or "."
4. "@." is not allowed to appear. Or. @
5. The end must not be the character "@" or "."
So based on the above principles and the syntax in the table above, it's easy to get the template you need: "(\w) +[@]{1} (\w) +[.] {1,3} (\w) + "
Next, we'll look at this template, first "\w" indicates that the beginning character of a message can only be a word character that contains an underscore, so that the third condition is met, and the "[@]{1}" means that it should match the character "@" in an e-mail message, and that the condition one is satisfied; {1,3} "means to match at least 1 matches of up to 3 characters in an e-mail message.", which satisfies the second condition, the final "(\w) +" character of the template can only be a word character containing an underscore, satisfies the condition five, the "(\w) +" in the middle of the template satisfies the condition four.
Then we'll just call that function Checkexp ("(\w) +[@]{1} (\w) +[." {1} (\w) + ", to check the string) is good, if the return of true to indicate that the data is legitimate, otherwise it is not correct, how, simple. We can also write a template that verifies the ID number: "([0-9]) {15}"; Template for validating URLs: "^http://{1} ((\w) +[.]) {1,3} "and so on; we can see that these templates provide us with a good reusable module, using their own or other provided by the various templates, we can easily and quickly verify the validity of the data, I believe you will write a very common template.
In this way, as long as we customize different templates, we can achieve the validity of different data validation. Therefore, the most important attribute of the regular expression object is: "pattern" attribute, as long as the real grasp of this attribute, you can freely use regular expression object to our data validation services.