我在表单上有多个文本字段,我希望其中两个应该验证,如果其中一个是空的,则说“两个字段都是必需的”。此外,我在表单上还有其他文本字段,他们已经在点击按钮时验证。
可以使用Asp.Net CustomValidator处理吗?
答案 0 :(得分:1)
您可以使用自定义验证程序执行此任务。
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator" ClientValidationFunction="testValid"
ControlToValidate="TextBox1" onservervalidate="CustomValidator1_ServerValidate"
ValidateEmptyText="True">both fields required</asp:CustomValidator>
ClientValidationFunction
包含客户端javascript函数testValid
。所以看起来应该是这样的:
<script type="text/javascript">
function testValid(sender, args) {
....you logic
//set args.IsValid according to your logic
args.IsValid = false;
}
</script>
在服务器端,
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
//set args.IsValid according to your validation logic.
args.IsValid = false;
}
答案 1 :(得分:0)
使用compare和require字段验证器,即
<label>Password</label> <asp:TextBox runat="server" ID="txtPassword" MaxLength="15" TextMode="Password" />
<label>Password-check</label> <asp:TextBox runat="server" ID="txtPasswordCheck" TextMode="Password" MaxLength="15" />
<asp:RequiredFieldValidator runat="server" ID="rfvtxtPasswordCheck" ControlToValidate="txtPasswordCheck" Text="* " />
<asp:CompareValidator runat="server" ID="cvtxtPasswordCheck" ControlToValidate="txtPasswordCheck" ControlToCompare="txtPassword" Operator="Equal" Type="String" Text="* Passwords do not match" />