2016-09-20 149 views
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> 

回答

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; 

}

+0

这怎么比我原来的功能有什么不同? – NoBullMan

+0

你的功能是准确的,但是调用该功能是错误的。调用函数onclientclick =“返回Functionname()”。那工作正常 –

相关问题