asp.net Verification Control Analysis _ practical skills

Source: Internet
Author: User
①, data Format validation control (RegularExpressionValidator)
Copy Code code as follows:

<asp:regularexpressionvalidator id= "RegularExpressionValidator1" runat= "Server" errormessage= "This username has already been registered" ControlToValidate = "Txtname" validationexpression = "\s+@\s+\.\s+" ></asp:RegularExpressionValidator>

A pattern match is made to the control you want to validate by customizing the regular expression.
Take the previous piece of code for example:
The ErrorMessage attribute indicates that the error message occurs when the validation is not valid;
The ControlToValidate property represents the ID of the control that needs to be validated;
The ValidationExpression property is specified as a regular expression of the validation condition;
Paste the common regular expression characters and their meanings here:
Copy Code code as follows:

Number Regular expression character meaning
1 [...] matches any one of the characters in parentheses
2 [^ ...] matches any one character not in parentheses
3 \w matches any one character (A~z, A~z and 0~9)
4 \w match any one whitespace character
5 \s matches any non-white-space character
6 \s matches any non-word character
7 \d Match any one number (0~9)
8 \d Match any non-digit (^0~9)
9 [\b] matches a backspace key character
Ten {n,m} matches the preceding expression n times at least, the maximum is m times
One {n,} matches the preceding expression n times at least
The {n} exactly matches the preceding expression n times
13? Match the preceding expression 0 or 1 times {0,1}
14 + at least match the preceding expression 1 times {1}
15 * Match the preceding expression at least 0 times {0}
16 | Match the preceding expression or the following expression
17 (...) Group items in a unit
18 ^ The beginning of a matching string
19 $ match End of string
\b Matching character bounds
\b A location that matches a non-character boundary

At the same time, enumerate several commonly used regular expressions: (some of which I have used, some of which are found on the Internet)
Copy Code code as follows:

Verify e-mail:
"\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * "
\s+@\s+\.\s+
Verify URL:
"\s+://\s+\.\s+"
Verify ZIP Code: "\d{6}"
Other common regular Expressions:
Represents 0~9 10 digits: "[0-9]"
Represents any number: "\d*"
Fixed telephone number for mainland China: "\d{3,4}-\d{7,8}"
Verify the ID number consisting of two digits, one hyphen, and 5 digits: "\d{2}-\d{5}"

②, custom validation controls (CustomValidator)
Default page:
Copy Code code as follows:

<asp:customvalidator id= "CustomValidator1" runat= "Server" errormessage= "This username has been registered" ControlToValidate = "Txtname" OnServerValidate = "Validatename" >
</asp:CustomValidator>

Defalult.cs page:
Copy Code code 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;
}
}
}

By customizing the server-side function code, it associates with the foreground validation control to form a false client-the effect of the current page validation.
In front of the previous validation control this code as an example:
The ErrorMessage attribute indicates that the error message occurs when the validation is not valid;
The ControlToValidate property represents the ID of the control that needs to be validated;
The OnServerValidate attribute representation is associated with the custom function to perform validation on the server;

The effect is as follows:

Excerpt from secretly in Bloom
Related Article

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.