带有onchange事件的ASP.net验证器问题

时间:2014-02-24 14:12:37

标签: javascript asp.net requiredfieldvalidator

我正在研究我正在开发的基于Web的应用程序的使用历史。应该发生什么:

如果用户输入第二家公司的公司名称,则需要第二家公司的某些其他字段。如果第二个公司名称留空,则其他字段验证器将保持禁用状态。

在大多数情况下,这没有任何问题;但是,当有人在第二个公司名称框中输入内容并点击下一步时,它会激活验证器。如果这不正确并且它们删除了在第二个公司名称框中键入的内容,则验证程序不会再次被禁用。

我在文本框中使用onchange事件调用javascript函数来启用和禁用验证器,我不确定它为什么不起作用。

这是我正在验证的文本框。非常感谢任何帮助。

谢谢,

  <asp:TextBox ID="txtCompany2" runat="server" onchange="validateCompany3()" />
    <asp:RequiredFieldValidator ID="rfvCompany2" runat="server"
            ControlToValidate="txtCompany2"
            errormessage="Please indicate which company you worked for."
            Enabled="false"
            ForeColor="Red"
            Text="*">
   </asp:RequiredFieldValidator> 

这是我的javascript

function validateCompany2(source, args) {
    var company = document.getElementById("<%=txtCompany2.ClientID %>");
    var valCompany2 = document.getElementById("<%=rfvCompany2.ClientID %>");
    var valCompany2City = document.getElementById("<%=rfvCompany2City.ClientID %>");
    var valCompany2State = document.getElementById("<%=rfvCompany2State.ClientID %>");
    var valCompany2Phone = document.getElementById("<%=rfvCompany2Phone.ClientID %>");
    var valCompany2Supervisor = document.getElementById("<%=rfvCompany2Supervisor.ClientID %>");
    var valCompany2Start = document.getElementById("<%=rfvCompany2StartDate.ClientID %>");
    var valCompany2End = document.getElementById("<%=rfvCompany2EndDate.ClientID %>");
    var valCompany2Job = document.getElementById("<%=rfvCompany2JobTitle.ClientID %>");
    var valCompany2StartSal = document.getElementById("<%=rfvCompany2StartingSalary.ClientID %>");
    var valCompany2EndSal = document.getElementById("<%=rfvCompany2EndingSalary.ClientID %>");
    var valCompany2Resp = document.getElementById("<%=rfvCompany2Responsibilities.ClientID %>");
    var valCompany2Reason = document.getElementById("<%=rfvCompany2ReasonForLeaving.ClientID %>");

    if (company == "" || company == null || company.value == null)
    {
        ValidatorEnable(valCompany2, false);
        ValidatorEnable(valCompany2City, false);
        ValidatorEnable(valCompany2State, false);
        ValidatorEnable(valCompany2Phone, false);
        ValidatorEnable(valCompany2Supervisor, false);
        ValidatorEnable(valCompany2Start, false);
        ValidatorEnable(valCompany2End, false);
        ValidatorEnable(valCompany2Job, false);
        ValidatorEnable(valCompany2StartSal, false);
        ValidatorEnable(valCompany2EndSal, false); 
        ValidatorEnable(valCompany2Resp, false);
        ValidatorEnable(valCompany2Reason, false);
    }
    else 
    {
        ValidatorEnable(valCompany2, true);
        ValidatorEnable(valCompany2City, true);
        ValidatorEnable(valCompany2State, true);
        ValidatorEnable(valCompany2Phone, true);
        ValidatorEnable(valCompany2Supervisor, true);
        ValidatorEnable(valCompany2Start, true);
        ValidatorEnable(valCompany2End, true);
        ValidatorEnable(valCompany2Job, true);
        ValidatorEnable(valCompany2StartSal, true);
        ValidatorEnable(valCompany2EndSal, true); 
        ValidatorEnable(valCompany2Resp, true);
        ValidatorEnable(valCompany2Reason, true);
    }
}

0 个答案:

没有答案