0
我有一個文本框來保存電話號碼和列出移動服務提供商的下拉列表。我試圖確保兩者都被選中。我使用客戶端驗證程序,使用驗證組進行驗證。相同的驗證組被分配到「更新」按鈕。客戶端自定義驗證
當我添加電話號碼並且沒有移動提供商時,反之亦然,只要控件失去焦點,就會顯示錯誤消息。但是,如果我點擊「更新」按鈕,則不會顯示錯誤,並且更新會很愉快地進行更新。看不到我做錯了什麼。文本框或下拉列表均未設置自動回覆。
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>
這怎麼比我原來的功能有什麼不同? – NoBullMan
你的功能是準確的,但是調用該功能是錯誤的。調用函數onclientclick =「返回Functionname()」。那工作正常 –