我有一个文本框,用于存放列出移动提供商的电话号码和下拉列表。我正在努力确保两者都被选中。我使用客户端验证的客户验证器,使用验证组。相同的验证组被分配给"更新"按钮。
当我添加电话号码而没有移动提供商时,反之亦然,一旦控制失去焦点,就会显示错误消息。但是,如果我点击"更新"按钮,没有显示错误,并且愉快地更新继续更新。无法看清我做错了什么。文本框或下拉列表都没有设置自动后备功能。
function ValidateMobile(oSrc, args) {
var tbMobile = document.getElementById('<%=tbMobile.ClientID%>');
var ddlMobileProvider = document.getElementById('<%=ddlMobileProvider.ClientID%>');
args.IsValid = true;
var mobileNum = tbMobile.value.trim();
var selectedCarrierValue = ddlMobileProvider.options[ddlMobileProvider.selectedIndex].value;
if ((mobileNum != "" && selectedCarrierValue == "") || (mobileNum == "" && selectedCarrierValue != ""))
args.IsValid = false;
}
<asp:TextBox runat="server" ID="tbMobile" CssClass="NormalSmall" Width="95%" />
<ajaxToolkit:MaskedEditExtender runat="server" ID="mtbMobile" TargetControlID="tbMobile" Mask="(999) 999-9999" />
<asp:DropDownList runat="server" ID="ddlMobileProvider" Width="95%" DataSourceID="odsMobileProviders" DataTextField="CARRIERNAME" DataValueField="MOBILECARRIERID" AppendDataBoundItems="true">
<asp:ListItem Text="Select Mobile Provider ..." Value="" />
</asp:DropDownList>
<asp:ImageButton runat="server" ID="ibUpdate" ImageUrl="~/assets/images/buttons/Update.png" OnClick="ibUpdate_Click" CausesValidation="true" ValidationGroup="vgCustInfo" />
<asp:CustomValidator runat="server" ID="cvMobile" ControlToValidate="tbMobile" Display="Dynamic" ValidationGroup="vgCustInfo" ClientValidationFunction="ValidateMobile" ErrorMessage="Both Carrier and Mobile Number must be specified"></asp:CustomValidator>
答案 0 :(得分:0)
function ValidateMobile() {
var tbMobile = document.getElementById('<%=tbMobile.ClientID%>');
var ddlMobileProvider = document.getElementById('<%=ddlMobileProvider.ClientID%>');
args.IsValid = true;
var mobileNum = tbMobile.value.trim();
var selectedCarrierValue = ddlMobileProvider.options[ddlMobileProvider.selectedIndex].value;
if ((mobileNum != "" && selectedCarrierValue == "") || (mobileNum == "" && selectedCarrierValue != ""))
args.IsValid = false;
}