Data validation involves a lot of things, in the current technical field, technically speaking is divided into traditional validation (synchronous mode) and Ajax (asynchronous) validation. If you look at it from functional verification, it's all inclusive. such as non-null verification, peer validation, format validation, and so on. There are also several common validation controls that are specifically used for validation in. NET, RequiredFieldValidator, Rangevalidator,regularexpressionvalidator,comparevalidator, CustomValidator and ValidationSummary, and so on, do not discuss their use and principles.
I. Traditional methods of verification
In traditional validation, a validation request is processed using the method of synchronizing form submissions. The shortcomings of this method of verification are obvious and cicada. Form submission One is that the amount of data transferred is more than the resource, and the second is that the page is synchronized. The page will be refreshed as the form is submitted. Here I look at a simple example of traditional validation:
Recommend a asp.net project, rename the default page to: traditvalidation.aspx, in the page. CS defines a simulation data in the following ways:
1/**//// <summary>
2/// 模拟一个数据字典.判断用户名是否存在
3/// </summary>
4/// <param name="strUsername"></param>
5/// <returns></returns>
6private bool IsUsernameExist(string strUsername)
7{
8 bool bRet = false;
9
10 switch (strUsername.ToLower())
11 {
12 case "beniao":
13 case "java":
14 case "admin":
15 case "ajax":
16 case "aspnet":
17 bRet = true; break;
18 }
19 return bRet;
20}
Next, go to Traditvalidation.aspx's HTML view and define a JavaScript method to validate the legality of the input data.
<script language="javascript">
function checkFrm()
{
var tbUsername = document.getElementById("tbUsername");
var msg = document.getElementById("lblMessage");
var re = /^(\w{3,15})$/gim;
if (re.test(tbUsername.value))
{
return true;
}
else
{
msg.innerHTML = "长度必须在3到15之间,且不包含字母、数字和下划线以外的字符!";
msg.style.color = "red";
return false;
}
}
</script>