ASP.NET检验控件的汇总

来源:互联网 发布:火车票购票软件 编辑:程序博客网 时间:2024/04/30 07:49

检验控件执行表单检查

1.RequiredFieldValidator (必填域)
Username:
<asp:TextBox
  ID="txtUsername"
  Text="Enter Some Text"   //初始值
  Runat="Server" />
<asp:RequiredFieldValidator
  Forecolor="Blue"     //颜色,不设置为红色
  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" />


 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕妇凌晨饿了怎么办 减肥想吃泡面怎么办 自考的实践考核怎么办 c1本怎么办从业资格证 办理资格证假的怎么办 高一学习差怎么办 职业资格证书理论考试不合格怎么办 职称证查不到怎么办 社保与工资不符怎么办 社保审计不过关怎么办 企安宝登录不上怎么办 苹果维修没发票怎么办 园林绿化资质取消后怎么办 出租车从业资格证到期怎么办 养老金认证身份证不符怎么办 硬盘指示灯不亮怎么办 做业务产品质量很差怎么办 没有户籍证明了怎么办 回执编号忘了怎么办 泳镜里面花了怎么办 网页打印预览空白怎么办 中专毕业证掉了怎么办 会计证年检忘了怎么办 会计准考证丢了怎么办 从业资格证没带怎么办 安全证过期了怎么办 安全员证掉了怎么办 局部抗浮不满足怎么办 职称代评被骗怎么办 租到了公租房怎么办 公租房不住了怎么办 公寓不退押金怎么办 找物业租房被骗怎么办 租房子被骗了怎么办 公租房怎么办入住手续 重庆公租房摇到号怎么办 重庆公租房摇到号后怎么办 我被网上起诉怎么办 摇号摇到了不买怎么办 保障房离婚了怎么办 深圳有房子户口怎么办