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 will be processed by the backend ASP. Program This may cause unnecessary troubles and even some security problems on 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, you can writeCodeIt is easier to analyze user input information one by one. If the analysis conditions are ever-changing, it will be miserable, not only do I have to write 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 and match create variables.
Set RegEx = new Regexp to create a regular expression.
RegEx. pattern = patrn setting mode.
RegEx. ignorecase = true sets whether to distinguish between uppercase and lowercase characters.
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.exe cute (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.
---------------------------------------------------------
This article is a post. Article . It is easy to understand ~