Objects | data | Just when we were making a website, in particular, a variety of e-commerce sites, the first will allow users to fill out a number of forms to obtain registered users of various information, because users may enter a wide range of information, and some do not meet the requirements of the data will give us back-end ASP processing program to bring unnecessary trouble, 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