Analysis of ASP.net verification controls

Source: Internet
Author: User

① Data format verification control (RegularExpressionValidator) Copy codeThe Code is as follows: <asp: regularExpressionValidator ID = "RegularExpressionValidator1" runat = "server" ErrorMessage = "this user name has been registered" ControlToValidate = "txtName" ValidationExpression = "\ S + @ \ S + \. \ S + "> </asp: RegularExpressionValidator>

Use a custom regular expression to match the pattern of the control to be verified.
The preceding code is used as an example:
The ErrorMessage attribute indicates an error message when the verification is invalid;
The ControlToValidate attribute indicates the ID of the control to be verified;
The ValidationExpression attribute specifies the regular expression used to verify the condition;
The following describes the characters and meanings of common Regular Expressions:Copy codeThe Code is as follows: Number, regular expression, character meaning
1 [……] Match any character in the brackets
2 [^…] Match any character not in parentheses
3 \ w match any character (~ Z, ~ Z and 0 ~ 9)
4 \ W matches any blank character
5 \ s match any non-blank character
6 \ S matches any non-word characters
7 \ d match any number (0 ~ 9)
8 \ D match any non-digit (^ 0 ~ 9)
9 [\ B] matches a backspace key character
10 {n, m} matches the previous expression n times at least, and the maximum is m times
11 {n,} matches the previous expression at least n times
12 {n} exactly matches the previous expression n times
13? Match the previous expression 0 or 1 time {0, 1}
14 + matches the previous expression at least once {1 ,}
15 * match the previous expression 0 at least {0 ,}
16 | match the expression above or below
17 (...) Combine projects in a unit
18 ^ match the start of a string
19 $ match the end of a string
20 \ B matching character Boundary
21 \ B matches a location with a non-character Boundary

At the same time, I will list several common Regular Expressions: (some of them are used by myself, and some are materials found online)Copy codeThe Code is as follows: Verify the email:
"\ W + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*"
\ S + @ \ S + \. \ S +
Verification URL:
"\ S +: // \ S + \. \ S +"
Verify the zip code: "\ d {6 }"
Other common Regular Expressions:
0 ~ 9 digits: "[0-9]"
Represents any number: "\ d *"
Landline phone number in mainland China: "\ d {3, 4}-\ d {7, 8 }"
Verify the ID number consisting of two digits, one hyphen and five digits: "\ d {2}-\ d {5 }"

② Custom verification control (CustomValidator)
Default Page:Copy codeThe Code is as follows: <asp: CustomValidator ID = "CustomValidator1" runat = "server" ErrorMessage = "this user name has been registered" ControlToValidate = "txtName" OnServerValidate = "ValidateName">
</Asp: CustomValidator>

Defalult. cs page:Copy codeThe Code is as follows: public void ValidateName (Object sender, ServerValidateEventArgs args)
{
SqlConnection myConn = new SqlConnection ("Data Source = (local); Initial Catalog = Csharp; Integrated Security = True ");
MyConn. Open ();
SqlCommand myCmd = new SqlCommand ("select use_account from users", myConn );
SqlDataAdapter myDa = new SqlDataAdapter (myCmd );
DataSet myDs = new DataSet ();
MyDa. Fill (myDs );
For (int I = 0; I <myDs. Tables [0]. Rows. Count; I ++)
{
If (args. Value. ToString () = myDs. Tables [0]. Rows [I] [0]. ToString ())
{
Args. IsValid = false;
Break;
}
Else
{
Args. IsValid = true;
}
}
}

The function code of the custom server is associated with the foreground verification control to form a fake client-current page verification effect.
The code for the front-end verification control is as follows:
The ErrorMessage attribute indicates an error message when the verification is invalid;
The ControlToValidate attribute indicates the ID of the control to be verified;
The OnServerValidate attribute indicates that it is associated with a custom function to perform verification on the server;

The effect is as follows:

From the dark bloom

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.