Using "Regular Expression Objects" in ASP to verify the legality of data

Source: Internet
Author: User

Using "Regular Expression Objects" in ASP to verify the legality of data

When we make the website, especially the various e-commerce websites, we will let the user fill in some forms to obtain the various information of the registered user, because the user may enter a variety of information, and some non-conforming data will bring unnecessary trouble to our backend ASP processing program. It even leads to some security issues on the site. Therefore, before we save this information to the database of the website, we need to verify the data of the information entered by these users so that the subsequent program can be executed safely and smoothly. Therefore, we usually write an ASP verification program on the backend to analyze whether the data entered by the user is legal.

Perhaps someone will ask, using JavaScript running on the client is not a better way to verify the user's data? Indeed, this is possible in most cases, and why is it most of the case? Because the JavaScript you write doesn't have to be completely normal to run on IE and Netscape, because Microsoft's JScript is not all the same as JavaScript, plus some browsers are not necessarily compatible with Microsoft and Netscape, So it is possible that the client's JavaScript does not accurately verify the user input of the various data, and the ASP program is run on the server side, and only the environment of your server, regardless of the client is what browser, for your ASP program is not separate, Therefore, it is a good choice to use the back-end ASP program to verify the validity of the data.

When using ASP to validate the data of the backend, some people write a lot of functions to satisfy the data check in different environment, for example, we want to verify whether the URL address entered by the user is legal, it is possible to write a piece of code to analyze the user input information by character. To analyze the amount of information is small, that is better to do, if the analysis of the conditions of the ever-changing, 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 is the "regular expression" object provided by VBScritp5.0, as long as your server has ie5.x installed, it will take VBScript5.0. In fact, the "regular expression" was originally a Unix patent, especially in the Perl language, the most widely used, it is because of the "regular expression" of the powerful function, so that Microsoft slowly transferred the regular expression object to the Windows system above, using

"Regular Expression" object, we can be very convenient to verify the legitimacy of the various data.

First, let's look at what is VBScript's "Regular expression" object, and let's start with a program:

Function checkexp (PATRN, STRNG)

Dim regEx, Match ' establishes a variable.

Set regex = New RegExp ' establishes a regular expression.

Regex.pattern = Patrn ' setup mode.

Regex.ignorecase = True ' Sets whether character case is distinguished.

Regex.global = True ' to set 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 assign the regular matching template to the object, that is, to tell the regular expression object, you want to match a template of what kind of child, Then using the method test to detect whether the data to be processed is exactly the same as the template we gave, if it does not match, it indicates that the data to be processed is not a legitimate data, and thus the validation of the data legitimacy, we can see that the use of a reasonable design matching template, We can easily verify a batch of similar data information.

Of course, VBScript5.0 in the "Regular expression" object There are many other methods and properties, such as the method of replace (), using him we can quickly implement the online very fashionable UBB style forum and BBS, which is not within the scope of our discussion, and later discussed, Now let's look at the common methods and properties of regular expression objects in data validation:


Common methods: Execute method

Description: Performs a regular expression search on the specified string.

Syntax: Object. The syntax for the Execute (String) Execute method consists of the following sections:

Object: Required. Always a name for the RegExp object.

String: Required. The text string on which to execute the regular expression.

Description: The design pattern for regular expression search is set by the pattern of the RegExp object. The Execute method returns a

The Matches collection, which contains each matching match object found in the 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 value indicating whether a matching pattern is found.

Syntax: Object. Test (String)

The syntax of the Test method consists of the following sections:

Object: Required. Always a name for the RegExp object.

String: Required. The text string to perform the regular expression search.


Description: The actual pattern of regular expression searches is set by the Pattern property of the RegExp object. The Regexp.global property has no effect on the test method. If a matching pattern is found, the test method returns True, otherwise false is returned.


Common Properties: Global properties

Description: Sets or returns a Boolean value that indicates whether the pattern matches the entire search string or only the first one.

Syntax: Object. Global [= True | False]

The object parameter is always Re

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.