现在我有一个asp.net 2.0应用程序,允许用户通过以下字段进行搜索
Location (Required if there is nothing in idnumber field)
Address (Required if there is nothing in idnumber field)
Zip (Required if there is nothing in idnumber field)
**OR**
IDNumber. (Required if there is nothing in any of the other fields)
我希望能够做的是在点击按钮时验证此客户端并显示错误摘要。
即。 如果用户将每个条件留空。我想显示“您必须输入IDNumber或”位置,地址和邮编继续“
我从未使用自定义验证控件,所以这里有一些问题。 1)它能够做到这一点吗? 2)有没有人有如何做到这一点的例子?
由于
答案 0 :(得分:1)
您可以使用ClientValidationFunction
控件的CustomValidator
属性来指定将验证表单的Javascript函数。您需要编写用于验证的JavaScript。除非您正在编写一个可以确保所有客户都启用了JavaScript的应用程序,否则我高度建议您还使用OnServerValidate
属性来提供服务器端验证。
答案 1 :(得分:0)
使用自定义验证器非常简单。在页面中添加一个,然后选择ServerValidate
事件,这将生成这样的函数(C#中的示例):
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
// Your validation logic goes here... set args.IsValid = true if it passes,
// false otherwise. Here is an example...
args.IsValid = false;
if(txtIDNumber.Text.Length > 0)
{
args.IsValid = true;
}
else if (txtLocation.Text.Length > 0
&& txtAddress.Text.Length > 0
&& txtZip.Text.Length > 0)
{
args.IsValid = true;
}
}