ASP.NET检验控件的汇总

ASP.NET检验控件的汇总


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


  Font-Name="仿宋体"   //显示字体
  ControlToValidate="txtUsername"
  Text="You must enter a username!"
  InitialValue="Enter Some Text"  //避免和初始值相同
  Runat="Server" />
2.RegularExpressionValidator (检验输入的有效性)
Product Code:
<asp:TextBox
  id="txtProductCode"
  Runat="Server"/>
<asp:RegularExpressionValidator
  ControlToValidate="txtProductCode"
  Text="Invalid Product Code!"
  ValidationExpression="P[0-9]{4}"  //P开头+4个数字为有效输入
  Runat="Server" />
Email Address:
<asp:TextBox
  id="txtEmail"
  Columns="50"
  Runat="Server"/>
<asp:RegularExpressionValidator
  ControlToValidate="txtEmail"
  Text="Invalid Email Address!"
  ValidationExpression="/S+@/S+/./S{2,3}"  //检验Email的有效性
  Runat="Server" />
Password:
<asp:TextBox
  id="txtPassword"
  Columns="30"
  Runat="Server"/>
<asp:RegularExpressionValidator
  ControlToValidate="txtPassword"
  Display="Dynamic"
  Text="Your password must contain between 3 and 20 characters!"
  ValidationExpression="/w{3,20}"   //至少一个字母开头,并且包含一个数字和3-20个字符
  Runat="Server" />
<asp:RegularExpressionValidator
  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:
<asp:TextBox
  id="txtPhone"
  Columns="30"
  Runat="Server"/>
<asp:RegularExpressionValidator
  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:
<asp:TextBox
  id="txtHomepage"
  Columns="50"
  Runat="Server"/>
<asp:RegularExpressionValidator
  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)
<asp:TextBox
  id="txtLastname"
  Columns="50"
  Runat="Server"/>
<asp:RegularExpressionValidator
  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:
<asp:TextBox
  id="txtZipCode"
  Columns="8"
  Runat="Server"/>
<asp:RegularExpressionValidator
  ControlToValidate="txtZipCode"
  Display="Dynamic"
  Text="Invalid ZIP Code!"
  ValidationExpression="/d{5}"    //只允许5个数字
  Runat="Server" />
3.CompareValidator  (比较值)
Start Date:
<asp:TextBox
  id="txtStartDate"
  Columns="8"
  Runat="Server"/>
End Date:
<asp:TextBox
  id="txtEndDate"
  Columns="8"
  Runat="Server"/>
<asp:CompareValidator
  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:
<asp:TextBox
  id="txtBirthDate"
  Columns="10"
  Runat="Server"/>
<asp:CompareValidator
  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:
<asp:TextBox
  id="txtMeetingDate"
  Columns="10"
  Runat="Server"/>
<asp:RangeValidator
  ID="valgMeetingDate"
  ControlToValidate="txtMeetingDate"
  Display="Dynamic"
  Text="Date must be in the next 3 months!"
  Type="Date"
  Runat="Server" />
5.ValidationSummary   (错误汇总控件)
错误显示在页面式:
<asp:ValidationSummary
  HeaderText="There are problems with the following
    form fields:"
  Runat="Server" />
First Name:
<asp:TextBox
  ID="txtFirstname"
  Runat="Server" />
<asp:RequiredFieldValidator
  ID="reqVal1"
  ControlToValidate="txtFirstname"
  Text="You must enter a first name!"
  ErrorMessage="First Name"
  Runat="Server" />
Last Name:
<asp:TextBox
  ID="txtLastname"
  Runat="Server" />
<asp:RequiredFieldValidator
  ID="reqVal2"
  ControlToValidate="txtLastname"
  Text="You must enter a last name!"
  ErrorMessage="Last Name"
  Runat="Server" />
错误弹出窗口式:
<asp:ValidationSummary
  ShowMessageBox="True"
  HeaderText="There are problems with the following
    form fields:"
  Runat="Server" />
First Name:
<asp:TextBox
  ID="txtFirstname"
  Runat="Server" />
<asp:RequiredFieldValidator
  ControlToValidate="txtFirstname"
  Text="You must enter a first name!"
  ErrorMessage="First Name"
  Runat="Server" />
Last Name:
<asp:TextBox
  ID="txtLastname"
  Runat="Server" />
<asp:RequiredFieldValidator
  ControlToValidate="txtLastname"
  Text="You must enter a last name!"
  ErrorMessage="Last Name"
  Runat="Server" />

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页