概述
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
1)必须字段验证控件(RequiredFieldValidator)用于保证该字段不为空;代码如下:
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server"></asp:Label>
<p>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="必须字段不能为空"></asp:RequiredFieldValidator>
</p>
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click1" Text="验证" />
</form>
</body>
</html>
主要将控件的ControlToValidate属性选择TextBox1,就算没有选择,浏览器也会提示
“RequiredFieldValidator1”的 ControlToValidate 属性不能为空。
<div>
</div>
<asp:Label ID="Label1" runat="server" height="22px" Text="请输入密码:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" height="22px" Text="请再次输入密码:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBox2" ControlToValidate="TextBox1"
ErrorMessage="两次输入的密码不一致"></asp:CompareValidator>
<br />
<br />
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="确定" />
<asp:Label ID="Label3" runat="server"></asp:Label>
</form>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="请输入100-1000的整数" MaximumValue="1000"
MinimumValue="100" Type="Integer"></asp:RangeValidator>
<br />
<br />
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="确定" />
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="输入一个有效的邮箱:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="你输入的不是有效的邮箱"
ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*"></asp:RegularExpressionValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
<title></title>
<script type="text/javascript">
function validateNumber(oSrc,args)
{
args.IsValid = (args.value % 5 == 0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="输入错误"
ClientValidationFunction="validateNumber"></asp:CustomValidator>
<br />
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
function validateNumber(oSrc,args)
{
args.IsValid = (args.value % 5 == 0);
}
</script>
<div>
<asp:Label ID="Label1" runat="server" Text="姓名:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="姓名不能为空">*</asp:RequiredFieldValidator>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="年纪:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="年龄不能为空">*</asp:RequiredFieldValidator>
</div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</form>
----------------------Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
最后
以上就是甜蜜春天为你收集整理的黑马程序员——验证控件的全部内容,希望文章能够帮你解决黑马程序员——验证控件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复