简体   繁体   English

客户端自定义验证

[英]Client side custom validation

I have a text box to hold phone number and drop down list that lists mobile providers. 我有一个文本框来保存电话号码,并在下拉列表中列出移动提供商。 I am trying to make sure that both are selected. 我试图确保两者都被选中。 I use a customer validator with client side validation, using a validation group. 我通过验证组将客户验证器与客户端验证一起使用。 Same validation group is assigned to an "Update" button. 将相同的验证组分配给“更新”按钮。

When I add phone number and no mobile provider, or vice versa, an error message is displayed as soon as control loses focus. 当我添加电话号码并且没有移动服务提供商时,反之亦然,一旦控件失去焦点,就会显示一条错误消息。 But if I click the "Update" button, no error is displayed and update happily goes on updating. 但是,如果我单击“更新”按钮,则不会显示任何错误,并且更新会愉快地继续进行。 Can't see what I am doing wrong. 看不到我在做什么错。 None of the textbox or drop down have autopostback set. 文本框或下拉菜单均未设置自动回发。

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>
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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM