Jquery验证无效

时间:2014-08-06 07:39:30

标签: jquery html asp.net

我已经在asp下拉列表和复选框上实现了一些验证。我已经在按钮点击上应用了一些jquery验证,但它的代码不起作用。我试过检查控制台,但它没有给我任何错误。请参阅代码供您参考:

<tr>
    <td style="padding: 12px;">Current Experience<span style="color: #CF060D;">*</span>
    </td>
    <td class="cls4">
        <asp:DropDownList ID="ddlCurrentExpYears" CssClass="txtfld-popup_DropD1" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="ddlCurrentExpMonth" CssClass="mrg1" runat="server"></asp:DropDownList>
        <asp:RequiredFieldValidator CssClass="error_msg" ID="reqCurrentMonths" runat="server" ControlToValidate="ddlCurrentExpMonth" ErrorMessage="Please select Current Experience" InitialValue="Months" SetFocusOnError="true"></asp:RequiredFieldValidator>
    </td>
</tr>
<tr>
    <td style="padding: 12px;">Total Experience<span style="color: #CF060D;">*</span>
    </td>
    <td class="cls3">
        <asp:DropDownList ID="ddlTotalExpYears" CssClass="txtfld-popup_DropD1" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="ddlTotalExpMonths" CssClass="mrg1" runat="server"></asp:DropDownList>
        <asp:RequiredFieldValidator CssClass="error_msg" ID="reqTotalExpMonths" runat="server" ControlToValidate="ddlTotalExpMonths" ErrorMessage="Please select Total Experience" InitialValue="Months" SetFocusOnError="true"></asp:RequiredFieldValidator><span id="lblExp"></span>
    </td>
</tr>
<asp:CheckBox ID="chkDisclaimer" runat="server" /><a href="pdfs/footers/Disclaimer.pdf" target="_blank" class="link1">I have read and agree to the terms and conditions mentioned in the disclaimer</a>

另外,请参阅jss小提琴以供参考: - http://jsfiddle.net/95M8S/1/

1 个答案:

答案 0 :(得分:0)

通过自己调试得到答案,忙碌了三个小时

<script type="text/javascript">

   function pageLoad() {

       $('#ctl00_ContentPlaceHolder1_txtOther').hide();
       $('#ctl00_ContentPlaceHolder1_txtOtherPG').hide();
       $('#ctl00_ContentPlaceHolder1_ddlGraduation').change(function () {
           if ($(this).val() === "Other") {
               $('#ctl00_ContentPlaceHolder1_txtOther').show();
               //Enable the validator when textbox is shown
               ValidatorEnable(document.getElementById("<%=reqGraduation.ClientID %>"), true);
            }
            else {
                $('#ctl00_ContentPlaceHolder1_txtOther').hide();
                //Disable the validator when textbox is hidden
                ValidatorEnable(document.getElementById("<%=reqGraduation.ClientID %>"), false);
            }
        });

        $('#ctl00_ContentPlaceHolder1_ddlPostGraduation').change(function () {
            if ($('#ctl00_ContentPlaceHolder1_ddlPostGraduation').val() === "Other") {
                $('#ctl00_ContentPlaceHolder1_txtOtherPG').show();
            }
            else {
                $('#ctl00_ContentPlaceHolder1_txtOtherPG').hide();
            }
        });

       $("#ctl00_ContentPlaceHolder1_txtDOB").datepicker({
           changeMonth: true,
           changeYear: true,
           dateFormat: 'MM dd, yy',
           minDate: "-58Y",
           maxDate: "-10Y",
           yearRange: "-58:-10",
           showOn: "button",
           buttonImage: "images/cal.gif",
           buttonImageOnly: true,
           showOn: "both"
       });
       $("#ctl00_ContentPlaceHolder1_txtDOB").on('keydown', function (e) {
           e.preventDefault();
           e.stopImmediatePropagation();
           return false;
       });
       $("#ctl00_ContentPlaceHolder1_txtDOB").on('cut copy paste', function (e) {
           e.preventDefault();
           e.stopImmediatePropagation();
           return false;
       });
       $('.ui-datepicker-trigger').css({ 'padding-top': '11px', })
       $("#ctl00_ContentPlaceHolder1_txtGradDate").datepicker({
           changeMonth: true,
           changeYear: true,
           dateFormat: 'MM dd, yy',
           maxDate: "0",
           yearRange: "-50:+1",
           showOn: "button",
           buttonImage: "images/cal.gif",
           buttonImageOnly: true,
           showOn: "both"
       });
       $("#ctl00_ContentPlaceHolder1_txtGradDate").on('keydown', function (e) {
           e.preventDefault();
           e.stopImmediatePropagation();
           return false;
       });
       $("#ctl00_ContentPlaceHolder1_txtGradDate").on('cut copy paste', function (e) {
           e.preventDefault();
           e.stopImmediatePropagation();
           return false;
       });
       $('.ui-datepicker-trigger').css({ 'padding-top': '11px', })
       $("#ctl00_ContentPlaceHolder1_txtPGGradDate").datepicker({
           changeMonth: true,
           changeYear: true,
           dateFormat: 'MM dd, yy',
           maxDate: "0",
           yearRange: "-50:+1",
           showOn: "button",
           buttonImage: "images/cal.gif",
           buttonImageOnly: true,
           showOn: "both"
       });
       $("#ctl00_ContentPlaceHolder1_txtPGGradDate").on('keydown', function (e) {
           e.preventDefault();
           e.stopImmediatePropagation();
           return false;
       });
       $("#ctl00_ContentPlaceHolder1_txtPGGradDate").on('cut copy paste', function (e) {
           e.preventDefault();
           e.stopImmediatePropagation();
           return false;
       });
       $('.ui-datepicker-trigger').css({ 'padding-top': '11px', })
       $('#lblDOB').text("");
       $('#lblGradDate').text("");
       $('#lblPGGradDate').text("");
       $('#lblExp').text("");
       $('#lblLocationPref').text("");
       $('#ctl00_ContentPlaceHolder1_ddlCountry').change(function () {
           $('#ctl00_ContentPlaceHolder1_btnChangeValue').click();
           $('#ctl00_ContentPlaceHolder1_btnChangeValue').click();
       });
       $('#ctl00_ContentPlaceHolder1_ddlState').change(function () {
           $('#ctl00_ContentPlaceHolder1_btnChangeValue').click();
           $('#ctl00_ContentPlaceHolder1_btnChangeValue').click();
       });
       $('#ctl00_ContentPlaceHolder1_chkRelocateYes').change(function () {
           var ischecked = $('#ctl00_ContentPlaceHolder1_chkRelocateYes:checked');
           if (ischecked.length == 0) {
               $('#ctl00_ContentPlaceHolder1_listLocationPref input[type=checkbox]').attr('disabled', 'disabled');
           }
           else {
               $('#ctl00_ContentPlaceHolder1_listLocationPref input[type=checkbox]').removeAttr('disabled');
           }
       });
       $('#ctl00_ContentPlaceHolder1_chkRelocateNo').change(function () {
           var ischecked = $('#ctl00_ContentPlaceHolder1_chkRelocateYes:checked');
           if (ischecked.length == 0) {
               $('#ctl00_ContentPlaceHolder1_listLocationPref input[type=checkbox]').attr('disabled', 'disabled');
           }
           else {
               $('#ctl00_ContentPlaceHolder1_listLocationPref input[type=checkbox]').removeAttr('disabled');
           }
       });
   $("#ctl00_ContentPlaceHolder1_btnSubmit").on("click", function () {
       $('#lblDOB').text("");
       $('#lblGradDate').text("");
       $('#lblPGGradDate').text("");
       $('#lblExp').text("");
       $('#lblLocationPref').text("");

       var currentyr = $("#ctl00_ContentPlaceHolder1_ddlCurrentExpYears").val();
       var currentmonth = $("#ctl00_ContentPlaceHolder1_ddlCurrentExpMonth").val();
       var totalyr = $("#ctl00_ContentPlaceHolder1_ddlTotalExpYears").val();
       var totalmonth = $("#ctl00_ContentPlaceHolder1_ddlTotalExpMonths").val();
       var currentexp = parseInt(currentyr * 12) + parseInt(currentmonth);
       var totalexp = parseInt(totalyr * 12) + parseInt(totalmonth);
       if (currentexp > totalexp) {
           $('#lblExp').text("Current Experience cannot be greater than Total Experience");
           $("#ctl00_ContentPlaceHolder1_ddlCurrentExpYears").focus();
           $('#lblExp').css({ 'color': 'red' });
           return false;
       }
       else {
           $('#lblExp').text("");
       }
       var relocate = $("#ctl00_ContentPlaceHolder1_chkRelocateYes:checked");

       if (relocate.length == 1) {
           var checked = $("#ctl00_ContentPlaceHolder1_listLocationPref input[type=checkbox]:checked");
           if (checked.length > 3) {
               $('#lblLocationPref').text("You can select max 3 location preferences");
               $("#ctl00_ContentPlaceHolder1_listLocationPref").focus();
               $('#lblLocationPref').css({ 'color': 'red' });
               return false;
           }
       }
       var disclaimer = $("#ctl00_ContentPlaceHolder1_chkDisclaimer")[0].checked;
       if (!disclaimer) {
           alert("You must accept the terms and conditions.");
           return false;
       }
       var captcha = $("input[name$=CareerCaptcha]").val();
       if (captcha == undefined || captcha == "") {
           $('#ctl00_ContentPlaceHolder1_lblCaptchaError').text("Please enter captcha text.");
           $("input[name$=CareerCaptcha]").focus();
           $('#lblCaptchaError').css({ 'color': 'red' });
           return false;
       }
            return true;
        });
   }
</script>