ASP.NET检验控件的汇总

ASP.NET检验控件的汇总


检验控件执行表单检查
1.RequiredFieldValidator (必填域)
Username:
  ID="txtUsername"
  Text="Enter Some Text"   //初始值
  Runat="Server" />


  Font-Name="仿宋体"   //显示字体
  ControlToValidate="txtUsername"
  Text="You must enter a username!"
  InitialValue="Enter Some Text"  //避免和初始值相同
  Runat="Server" />
2.RegularExpressionValidator (检验输入的有效性)
Product Code:
  id="txtProductCode"
  Runat="Server"/>
  ControlToValidate="txtProductCode"
  Text="Invalid Product Code!"
  ValidationExpression="P[0-9]{4}"  //P开头+4个数字为有效输入
  Runat="Server" />
Email Address:
  id="txtEmail"
  Columns="50"
  Runat="Server"/>
  ControlToValidate="txtEmail"
  Text="Invalid Email Address!"
  ValidationExpression="/S+@/S+/./S{2,3}"  //检验Email的有效性
  Runat="Server" />
Password:
  id="txtPassword"
  Columns="30"
  Runat="Server"/>
  ControlToValidate="txtPassword"
  Display="Dynamic"
  Text="Your password must contain between 3 and 20 characters!"
  ValidationExpression="/w{3,20}"   //至少一个字母开头,并且包含一个数字和3-20个字符
  Runat="Server" />
  ControlToValidate="txtPassword"
  Display="Dynamic"
  Text="Your password must contain at least one number and letter!"
  ValidationExpression="(/w+/d+/w*)|(/w*/d+/w+)"
  Runat="Server" />
Phone Number:
  id="txtPhone"
  Columns="30"
  Runat="Server"/>
  ControlToValidate="txtPhone"
  Display="Dynamic"
  Text="Invalid Phone Number!"
  ValidationExpression="/(?/s*/d{4}/s*[/)/./-]?/s*/d{4}/s*[/-/.]?/s*/d{4}"
       //电话号码的检查,必须为(0754) 290-1314或0754.290.1314或0754 290 1314
  Runat="Server" />
Enter the address of your homepage:
  id="txtHomepage"
  Columns="50"
  Runat="Server"/>
  ControlToValidate="txtHomepage"
  Display="Dynamic"
  Text="Invalid URL!"
  EnableClientScript="False"  //禁用客户端检查
  ValidationExpression="(?i:http:///S+/./S+)"  //网址的检查
  Runat="Server" />
Enter your last name:(no more than 10 characters)
  id="txtLastname"
  Columns="50"
  Runat="Server"/>
  ControlToValidate="txtLastname"
  Display="Dynamic"
  Text="Your last name can contain a maximum of 10 characters and no spaces!"
  ValidationExpression="/S{0,10}"      //长度检查,不多于10字符
  Runat="Server" />
ZIP Code:
  id="txtZipCode"
  Columns="8"
  Runat="Server"/>
  ControlToValidate="txtZipCode"
  Display="Dynamic"
  Text="Invalid ZIP Code!"
  ValidationExpression="/d{5}"    //只允许5个数字
  Runat="Server" />
3.CompareValidator  (比较值)
Start Date:
  id="txtStartDate"
  Columns="8"
  Runat="Server"/>
End Date:
  id="txtEndDate"
  Columns="8"
  Runat="Server"/>
  ControlToValidate="txtEndDate"  //想要检验的控件
  ControlToCompare="txtStartDate" //用来比较值的控件
  Display="Dynamic"
  Text="End date must be greater than start date!"
  Operator="GreaterThan"   //使用的比较操作符,大于
  Type="Date"              //检验类型  如string,integer
  Runat="Server" />
Enter your birth date:
  id="txtBirthDate"
  Columns="10"
  Runat="Server"/>
  ControlToValidate="txtBirthDate"
  Display="Dynamic"
  Text="Invalid birth date!"
  Operator="DataTypeCheck"   //日期格式
  Type="Date"
  Runat="Server" />
4.RangeValidator   (检查值的范围)
后台:
void Page_Load(Object sender , EventArgs e)
{
  valgMeetingDate.MinimumValue = DateTime.Now.ToString("MM/dd/yyyy");
  valgMeetingDate.MaximumValue = DateTime.Now.AddMonths(3).ToString("MM/dd/yyyy");
}
前台:
Choose a meeting date in the next three months:
  id="txtMeetingDate"
  Columns="10"
  Runat="Server"/>
  ID="valgMeetingDate"
  ControlToValidate="txtMeetingDate"
  Display="Dynamic"
  Text="Date must be in the next 3 months!"
  Type="Date"
  Runat="Server" />
5.ValidationSummary   (错误汇总控件)
错误显示在页面式:
  HeaderText="There are problems with the following
    form fields:"
  Runat="Server" />
First Name:
  ID="txtFirstname"
  Runat="Server" />
  ID="reqVal1"
  ControlToValidate="txtFirstname"
  Text="You must enter a first name!"
  ErrorMessage="First Name"
  Runat="Server" />
Last Name:
  ID="txtLastname"
  Runat="Server" />
  ID="reqVal2"
  ControlToValidate="txtLastname"
  Text="You must enter a last name!"
  ErrorMessage="Last Name"
  Runat="Server" />
错误弹出窗口式:
  ShowMessageBox="True"
  HeaderText="There are problems with the following
    form fields:"
  Runat="Server" />
First Name:
  ID="txtFirstname"
  Runat="Server" />
  ControlToValidate="txtFirstname"
  Text="You must enter a first name!"
  ErrorMessage="First Name"
  Runat="Server" />
Last Name:
  ID="txtLastname"
  Runat="Server" />
  ControlToValidate="txtLastname"
  Text="You must enter a last name!"
  ErrorMessage="Last Name"
  Runat="Server" />

你可能感兴趣的:(ASP.NET)