Asp. NET form Verification method 1th/2 page _ Practical Tips

Source: Internet
Author: User
1. Use validation controls
This is client-side validation, and Microsoft developers encapsulate the most common validation features, making our development efficiency significantly higher, and especially the custom validation controls, so flexible that we can design validation logic ourselves. However, the validation control received the browser restrictions, remember that in a development process, using the Firefox browser to browse, found that all validation controls failed, this is not a asp.net design vulnerabilities, can only say that the browser standard is not the only cause.
Asp. NET public six kinds of validation controls, respectively, are as follows:
RequiredFieldValidator (mandatory field validation) is used to check for input values
CompareValidator (comparison validation) compares two inputs by setting
RangeValidator (range validation) Enter whether the specified range
RegularExpressionValidator (regular expression validation) regular expression validation control
CustomValidator (custom validation) custom validation controls
ValidationSummary (validation Summary) Summary validation Results
2, use JS verification
JS verification, appears earlier, also belongs to the client verification, its flexible, applicable performance won people's preferences.
3. Background program Verification
Although client-side validation is more flexible, its limitations are obvious and server end validation is essential.
(1) Security if malicious attackers, the client page validation controls and JS verification all erased, and the server side did not make effective verification processing, will bring great loopholes to the attackers, the consequences of unimaginable.
(2) The limitations of client authentication, such as user registration, the need to determine whether the user name exists, this needs to be judged from the database query.
(3) Powerful various verification functions can be implemented here.
Disadvantage: Increasing server-side burdens
4, the Ajax class verification
This combines both client and server-side validation. Because of the use of Ajax frameworks, performance is less costly, but the user experience and the simplicity of coding improved.

In the process of verification, a strong and flexible regular expression is needed for verification of different formats.

To validate with a regular expression:
When writing a program or Web page that handles strings, there is often a need to find strings that match some of the complex rules. Regular expressions are the tools used to describe these rules. In other words, regular expressions are code that records text rules.

For more information about regular expressions, see: http://www.regexlab.com/zh/regref.htm

Regular expressions for string processing, form verification and other occasions, practical and efficient, so its application is very wide, I will use a few examples to illustrate the application of regular expressions in the validation.

Here I will verify the format of the phone number as an example to introduce.
<1> Use JS Verification
Copy Code code as follows:

<asp:button id= "Button2" runat= "Server" text= "test" onclientclick= "return Checkcellphone ();" onclick= "Button2_Click"/>

Verify the phone's JS code
Copy Code code as follows:

<script type= "Text/javascript" >
function Checkcellphone ()//Verify Phone
{
var Mobile=document.getelementbyid ("Tbmobile"). Value;
if (mobile!= "")
{
var reg0=/^13\d{5,9}$/; 130--139. At least 7 digits
var reg1=/^153\d{4,8}$/; Unicom 153. At least 7 digits
var reg2=/^159\d{4,8}$/; Move 159. At least 7 digits
var reg3=/^158\d{4,8}$/; Move 158. At least 7 digits
if (reg0.test (mobile) | | Reg1.test (MOBILE) | | Reg2.test (MOBILE) | | Reg3.test (MOBILE))
{
return true;
}
Else
{
Alert ("Phone number format is wrong!") ");
document.getElementById ("Tbmobile"). focus ();
return false;
}
}
Else
{
Alert ("Cell phone number cannot be empty!") ");
document.getElementById ("Tbmobile"). focus ();
return false;
}
}
</script>

Current 1/2 page 12 Next read the full text
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.