我有三个文本框,我想验证它们.至少一个文本框必须包含数据.
I have three textboxes and I want to validate them. At least one textbox must contain data.
我该怎么做?
(文本框是家庭电话号码,工作电话号码,手机号码,我需要检查至少指定一种联系方式)
(The textboxes are Home Phone No., Work Phone No., Mobile No. and I need to check at least one method of contact is specified)
使用自定义验证器,无需循环浏览页面上的文本框,因为这种方法可以获取页面上的所有文本框.ClientValidationFunction
中指定的 JavaScript 函数将为每个带有关联验证器的文本框调用.
Use a Custom Validator, there is no need to cycle through the text boxes on the page as this approach gets ALL of the text boxes on the page. The JavaScript function specified in the ClientValidationFunction
will be called for each text box with a validator associated with it.
<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvHomePhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtHomePhone" ValidateEmptyText="true"></asp:CustomValidator>
<asp:TextBox ID="txtWorkPhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvWorkPhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtWorkPhone" ValidateEmptyText="true"></asp:CustomValidator>
<asp:TextBox ID="txtMobilePhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvMobilePhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtMobilePhone" ValidateEmptyText="true"></asp:CustomValidator>
<script language="javascript">
function Validate(sender, args)
{
args.IsValid = false;
if(args.Value != "")
{
args.IsValid = true;
}
}
</script>
这篇关于asp.net 验证文本框 - 至少一个文本框必须有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!