Validation control Introduction--regularexpressionvalidator

Source: Internet
Author: User
Tags character set contains expression integer numeric mail range regular expression
express| control when we make a website, especially a variety of e-commerce sites, first of all, we will let users fill out a number of forms to get registered users of various information, because users can enter a wide range of information, and some do not meet the requirements of the data will give us back-end ASP processing process 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.
Using the RegularExpressionValidator server control, you can check to see if the information we entered is consistent with our custom expression. For example, use it to check e-mail addresses, phone numbers, and other legality.
Before we talk about the use of RegularExpressionValidator server controls, let's take a look at the source of the regular expression (regularexpression):
The "ancestors" of regular expressions can be traced back to early studies of how the human nervous system works. Warren McCulloch and Walter Pitts, two neuroscientists, have developed a mathematical way of describing these neural networks. In 1956, an American mathematician named Stephen Kleene, based on the early work of McCulloch and Pitts, published a paper entitled "Representation of neural network events", introducing the concept of regular expressions. A regular expression is an expression that describes what he calls the algebra of a regular set, so the term "regular expression" is used. Subsequently, it was found that this work could be applied to some early studies using Ken Thompson's computational Search algorithm, and Ken Thompson was the main inventor of Unix. The first practical application of regular expressions is the QED editor in Unix. As they say, the rest is a well-known history. From then until now regular expressions are an important part of text-based editors and search tools.
In fact, a regular expression (regularexpression) is a literal pattern consisting of regular characters (such as characters A through Z) and special characters (called metacharacters). This pattern describes one or more strings to be matched when looking for a text body. A regular expression is used as a template to match a character pattern with the string being searched for.
Using regular expressions, you can:
1. Test a pattern for a string. For example, you can test an input string to see if there is a phone number pattern or a credit card number pattern in the string. This is known as data validation.
2. Replace text. You can use a regular expression in your document to identify specific text, and then you can delete it all, or replace it with another text.
3. Extracts a substring from a string based on pattern matching. Can be used to find specific text in text or input fields.
For example, if you need to search the entire Web site to remove some outdated material and replace some HTML formatting tags, you can use regular expressions to test each file to see if there are any material or HTML formatting tags that you want to find in the file. With this method, you can narrow the affected file range to those files that contain the material you want to delete or change. You can then use regular expressions to delete obsolete materials, and finally, you can use regular expressions again to find and replace those that need to be replaced.
Another example that illustrates the usefulness of regular expressions is a language whose string-handling power is not yet known. VBScript is a subset of Visual Basic that has rich string processing capabilities. Visual Basic scripting Edition similar to C does not have this capability. Regular expressions have significantly improved the ability of the Visual Basic scripting Edition to handle string processing. However, it may be more efficient to use regular expressions in VBScript, which allows multiple string operations to be performed in a single expression.
It is because of the powerful function of "regular expression" that Microsoft slowly porting regular expression objects to the Windows system. 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.
RegularExpressionValidator has two main properties for validation. The controltovalidate contains a value for validation. such as removing the value from the text box. such as controltovalidate= "TextBox1" validationexpression contains a regular expression for validation.
Well, with the above description, let's give an example of a regular expression. 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: test@yesky.com, 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. The first character must not be "@" or "."
3. "@." is not allowed to appear. Or. @
4. The end must not be the character "@" or "."
So based on the above principles and the syntax in the table above, we can easily get the template as follows: "=" ^\w+ (-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ (\.| -) [a-za-z0-9]+) *\. [a-za-z0-9]+$]
Please see the contents of validata6.aspx:

<!--source file: Form\web Page Introduction \validate6.aspx-->
<body>
<center><p>
<form runat= "Server" >
<center>
<title> use regular expression validation </title>
<table bgcolor= "#eeeeee" cellpadding=10>
&LT;TR valign= "Top" >
&LT;TD colspan=3>
<asp:label id= "lbloutput" text= "Input e-mail address" font-name= "Verdana" font-size= "10pt" runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD align=right>
<font Face=verdana size=2>e-mail:</font>
</td>
<td>
<asp:textbox id=textbox1 runat=server/>
</td>
<td>
<asp:regularexpressionvalidator id= "RegularExpressionValidator1" runat= "Server"
Controltovalidate= "TextBox1"
Validationexpression= "^\w+ (-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ (\.| -) [a-za-z0-9]+) *\. [a-za-z0-9]+$]
Display= "Static"
Font-name= "Verdana"
Font-size= "10pt" >
Please enter a valid e-mail address!
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:button text= "Verify" onclick= "Validatebtn_click" Runat=server/>
</td>
<td></td>
</tr>
</table>
</center>
</form>
</body>

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.


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.