Confirm form input with rule formulas

Source: Internet
Author: User
First, the introduction
Writing form code is something we often have to do, and often becomes very complex and messy. This article will explain how to use regular formulas to simplify your code.
Ii. question-making
Writing a form confirmation code is not just a matter of checking for null values or finding a specific data type, things can be a lot messier. A simple example requires the user to enter an e-mail address. You need to determine whether the user has entered a form such as A@B.C, not something else. This problem can be very complicated.
Third, the solution
It boils down to the use of regular expressions. Perhaps because the Uinx platform supports this technology from the outset, Microsoft has been touting it as a scripting platform without any fanfare. Here is the definition of Microsoft:
Regular expressions are text styles that consist of ordinary characters, such as letters A through Z, and special characters, such as metacharacters. A style describes one or more strings that are used to match. A rule style is a template that matches a string that is searched.
Iv. examples
Microsoft has done a good job of grammar detection. But, as expected, in practical cases, they are very short. Here's a simple example:
' Example 1
Function Validateemail (Expression)
Dim objRegExp
Set objRegExp = New RegExp
Objregexp.pattern = "^[w.-]+@[w.-]+." [a-za-z]+$]
Validateemail = Objregexp.test (Expression)
End Function
Example 1 is an example of detecting an e-mail address. At first glance, the style string is messy, and in fact it's not. Let's take a closer look at each of its elements.
^-Represents the search for a matching character starting at the first character entered.
[w.-]+-brackets represent the range of characters. "W" means that any command character can include an underscore, which is equivalent to "[a-za-z0-9_]". "." Represents an increase in the period to a character range. The period is a special symbol, which is why we want to add the brackets. "-" means adding hyphens to character ranges. "+" means match the preceding symbol
One or more times.
@-Obviously, we need to see the "@" character here.
[w.-]+-We have seen it once, as mentioned above.
. -Here we need to see "." Character.
[a-za-z]+-Here we can use "w" instead. But as far as I know, there is no use of numbers and underscores in the first-level domain name (. com,.net, etc.).
The $-"$" character indicates the end.
' Example 2
Function validatessn (Expression)
Dim objRegExp
Set objRegExp = New RegExp
Objregexp.pattern = "^d{3}-d{2}-d{4}$"
VALIDATESSN = Objregexp.test (Expression)
End Function
Example 2 is an example of a public key legality check. The "d{3}" syntax indicates whether the check is a three-digit number. The rest is simple.
V. Summary
As you can see from the example, using a rule formula simplifies your code. The only difficulty is learning style syntax. Hopefully my example will inspire you to use this powerful technology!

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.