Home > Blockchain >  Nested jquery "if else if" crashes
Nested jquery "if else if" crashes

Time:10-06

I have this block of javascript which is working just fine.

        $('#<%=Button_PayFees.ClientID%>').click(function (evt) {
            var valuefirstname = $('#<%=TextBox_FirstName.ClientID%>').val().toUpperCase();
            var valuelastname = $('#<%=TextBox_LastName.ClientID%>').val().toUpperCase();
            var valueaddress = $('#<%=TextBox_Address.ClientID%>').val().toUpperCase();
            var valuecity = $('#<%=TextBox_City.ClientID%>').val().toUpperCase();
            var valuestate = $('#<%=Dropdownlist_States.ClientID%>').val().toUpperCase();
            var valuezipcode = $('#<%=TextBox_ZipCode.ClientID%>').val().toUpperCase();
            var valuephone = $('#<%=TextBox_Phone.ClientID%>').val().toUpperCase();
            var valueEmail = $('#<%=TextBox_EmailAddress.ClientID%>').val().toUpperCase();
            var valuecreditcard = $('#<%=DropDownList_CreditCard.ClientID%>').val();
            var valuecreditcardnumber = $('#<%=TextBox_CreditCard.ClientID%>').val().toUpperCase();
            var valuecsvcode = $('#<%=TextBox_CSVCode.ClientID%>').val().toUpperCase();

            var valueparticipantfirstname = $('#<%=TextBox_ParticipantFirstName.ClientID%>').val().toUpperCase();
            var valueparticipantlastname = $('#<%=TextBox_ParticipantLastName.ClientID%>').val().toUpperCase();
            var valueTeamName = $("#<%=DropDownList_CheerLevel.ClientID%>").find("option:selected").text();
            var valueOrganization = $('#<%=TextBox_Organization.ClientID%>').val().toUpperCase();

            var valuedeliveryfirstname = $('#<%=TextBox_DeliveryFirstName.ClientID%>').val().toUpperCase();
            var valuedeliverylastname = $('#<%=TextBox_DeliveryLastName.ClientID%>').val().toUpperCase();
            var valuedeliveryaddress = $('#<%=TextBox_DeliveryAddress.ClientID%>').val().toUpperCase();
            var valuedeliverycity = $('#<%=TextBox_DeliveryCity.ClientID%>').val().toUpperCase();
            var valuedeliverystate = $('#<%=DropDownList_DeliveryStates.ClientID%>').val().toUpperCase();
            var valuedeliveryzipcode = $('#<%=TextBox_DeliveryZipCode.ClientID%>').val().toUpperCase();


            if(valueparticipantfirstname == '' ){
                alert('Participant firstname is required.');
                evt.preventDefault();
                $('#<%=TextBox_ParticipantFirstName.ClientID%>').focus();
            }
            else if (valueparticipantlastname == ''){
                alert('Participant lastname is required.');
                evt.preventDefault();
                $('#<%=TextBox_ParticipantLastName.ClientID%>').focus();
            }
            else if (valueOrganization == '') {
                alert('Organization is required.');
                evt.preventDefault();
                $('#<%=TextBox_Organization.ClientID%>').focus();
            }
            else if (valueTeamName == 'Select Cheer Level') {
                alert('Select Cheer Level from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_CheerLevel.ClientID%>').focus();
            }
            else if (selection == 'Select DVD Option') {
                alert('Please select a DVD option from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_Options.ClientID%>').focus();
            }
            else if (valuefirstname == '') {
                alert('Firstname is required.');
                evt.preventDefault();
                $('#<%=TextBox_FirstName.ClientID%>').focus();
            }
            else if (valuelastname == ''){
                alert('Lastname is required.');
                evt.preventDefault();
                $('#<%=TextBox_LastName.ClientID%>').focus();
            }
            else if(valueaddress == '') {
                alert('Address is required.');
                evt.preventDefault();
                $('#<%=TextBox_Address.ClientID%>').focus();
            }
            else if(valuecity == '') {
                alert('City is required.');
                evt.preventDefault();
                $('#<%=TextBox_City.ClientID%>').focus();
            }
            else if(valuestate == 'SELECT STATE') {
                alert('Select state from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_States.ClientID%>').focus();
            }
            else if(valuezipcode == '') {
                alert('Zip code is required.');
                evt.preventDefault();
                $('#<%=TextBox_ZipCode.ClientID%>').focus();

            }
            else if(valuephone == '') {
                alert('Phone is required.');
                evt.preventDefault();
                $('#<%=TextBox_Phone.ClientID%>').focus();
            }
            else if(valueEmail == '') {
                alert('Email Address is required.');
                evt.preventDefault();
                $('#<%=TextBox_EmailAddress.ClientID%>').focus();
            }
            else if(valuecreditcard == 'SELECT CARD TYPE') {
                alert('Select credit card type from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_CreditCard.ClientID%>').focus();
            }
            else if(valuecreditcardnumber == '') {
                alert('Enter credit card number.');
                evt.preventDefault();
                $('#<%=TextBox_CreditCard.ClientID%>').focus();
            }
            else if(valuecsvcode == '') {
                alert('Enter credit card ID code.');
                $('#<%=TextBox_CSVCode.ClientID%>').focus();
                evt.preventDefault();
            }
    });              

When I change it to add checks for delivery information when a checkbox is checked it crashes ... no idea why as I'm not getting any feed back from the site. The code that crashes is (note the nested else if):

        $('#<%=Button_PayFees.ClientID%>').click(function (evt) {
            var valuefirstname = $('#<%=TextBox_FirstName.ClientID%>').val().toUpperCase();
            var valuelastname = $('#<%=TextBox_LastName.ClientID%>').val().toUpperCase();
            var valueaddress = $('#<%=TextBox_Address.ClientID%>').val().toUpperCase();
            var valuecity = $('#<%=TextBox_City.ClientID%>').val().toUpperCase();
            var valuestate = $('#<%=Dropdownlist_States.ClientID%>').val().toUpperCase();
            var valuezipcode = $('#<%=TextBox_ZipCode.ClientID%>').val().toUpperCase();
            var valuephone = $('#<%=TextBox_Phone.ClientID%>').val().toUpperCase();
            var valueEmail = $('#<%=TextBox_EmailAddress.ClientID%>').val().toUpperCase();
            var valuecreditcard = $('#<%=DropDownList_CreditCard.ClientID%>').val();
            var valuecreditcardnumber = $('#<%=TextBox_CreditCard.ClientID%>').val().toUpperCase();
            var valuecsvcode = $('#<%=TextBox_CSVCode.ClientID%>').val().toUpperCase();

            var valueparticipantfirstname = $('#<%=TextBox_ParticipantFirstName.ClientID%>').val().toUpperCase();
            var valueparticipantlastname = $('#<%=TextBox_ParticipantLastName.ClientID%>').val().toUpperCase();
            var valueTeamName = $("#<%=DropDownList_CheerLevel.ClientID%>").find("option:selected").text();
            var valueOrganization = $('#<%=TextBox_Organization.ClientID%>').val().toUpperCase();

            var valuedeliveryfirstname = $('#<%=TextBox_DeliveryFirstName.ClientID%>').val().toUpperCase();
            var valuedeliverylastname = $('#<%=TextBox_DeliveryLastName.ClientID%>').val().toUpperCase();
            var valuedeliveryaddress = $('#<%=TextBox_DeliveryAddress.ClientID%>').val().toUpperCase();
            var valuedeliverycity = $('#<%=TextBox_DeliveryCity.ClientID%>').val().toUpperCase();
            var valuedeliverystate = $('#<%=DropDownList_DeliveryStates.ClientID%>').val().toUpperCase();
            var valuedeliveryzipcode = $('#<%=TextBox_DeliveryZipCode.ClientID%>').val().toUpperCase();


            if(valueparticipantfirstname == '' ){
                alert('Participant firstname is required.');
                evt.preventDefault();
                $('#<%=TextBox_ParticipantFirstName.ClientID%>').focus();
            }
            else if (valueparticipantlastname == ''){
                alert('Participant lastname is required.');
                evt.preventDefault();
                $('#<%=TextBox_ParticipantLastName.ClientID%>').focus();
            }
            else if (valueOrganization == '') {
                alert('Organization is required.');
                evt.preventDefault();
                $('#<%=TextBox_Organization.ClientID%>').focus();
            }
            else if (valueTeamName == 'Select Cheer Level') {
                alert('Select Cheer Level from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_CheerLevel.ClientID%>').focus();
            }
            else if (selection == 'Select DVD Option') {
                alert('Please select a DVD option from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_Options.ClientID%>').focus();
            }
            else if (valuefirstname == '') {
                alert('Firstname is required.');
                evt.preventDefault();
                $('#<%=TextBox_FirstName.ClientID%>').focus();
            }
            else if (valuelastname == ''){
                alert('Lastname is required.');
                evt.preventDefault();
                $('#<%=TextBox_LastName.ClientID%>').focus();
            }
            else if(valueaddress == '') {
                alert('Address is required.');
                evt.preventDefault();
                $('#<%=TextBox_Address.ClientID%>').focus();
            }
            else if(valuecity == '') {
                alert('City is required.');
                evt.preventDefault();
                $('#<%=TextBox_City.ClientID%>').focus();
            }
            else if(valuestate == 'SELECT STATE') {
                alert('Select state from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_States.ClientID%>').focus();
            }
            else if(valuezipcode == '') {
                alert('Zip code is required.');
                evt.preventDefault();
                $('#<%=TextBox_ZipCode.ClientID%>').focus();

            }
            else if(valuephone == '') {
                alert('Phone is required.');
                evt.preventDefault();
                $('#<%=TextBox_Phone.ClientID%>').focus();
            }
            else if(valueEmail == '') {
                alert('Email Address is required.');
                evt.preventDefault();
                $('#<%=TextBox_EmailAddress.ClientID%>').focus();
            }
            else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
                     else if (valuedeliveryfirstname == '') {
                         alert('Please enter the first name of the person the DVD is being delivered to.');
                         evt.preventDefault();
                         $('#<%=TextBox_DeliveryFirstName.ClientID%>').focus();
                     }
                     else if (valuedeliverylastname == '') {
                         alert('Please enter the last name of the person the DVD is being delivered to.');
                         evt.preventDefault();
                         $('#<%=TextBox_DeliveryLastName.ClientID%>').focus();
                     }
                     else if (valuedeliveryaddress == '') {
                         alert('Please enter the delivery Address.');
                         evt.preventDefault();
                         $('#<%=TextBox_DeliveryAddress.ClientID%>').focus();
                     }
                     else if (valuedeliverycity == '') {
                         alert('Please enter the delivery city.');
                         evt.preventDefault();
                         $('#<%=TextBox_DeliveryCity.ClientID%>').focus();
                     }
                     else if (valuedeliverystate == 'SELECT STATE') {
                         alert('Select the delivery state from the drop down list.');
                         evt.preventDefault();
                         $('#<%=DropDownList_DeliveryStates.ClientID%>').focus();
                     }
                     else if (valuedeliveryzipcode == '') {
                         alert('Please enter the delivery Zip code.');
                         evt.preventDefault();
                         $('#<%=TextBox_DeliveryZipCode.ClientID%>').focus();
                     }
            }
            else if(valuecreditcard == 'SELECT CARD TYPE') {
                alert('Select credit card type from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_CreditCard.ClientID%>').focus();
            }
            else if(valuecreditcardnumber == '') {
                alert('Enter credit card number.');
                evt.preventDefault();
                $('#<%=TextBox_CreditCard.ClientID%>').focus();
            }
            else if(valuecsvcode == '') {
                alert('Enter credit card ID code.');
                $('#<%=TextBox_CSVCode.ClientID%>').focus();
                evt.preventDefault();
            }
    });              

something in the nested else if causes the code to crash. I can't see it.

CodePudding user response:

This doesn't look right!

            else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
                     else if (valuedeliveryfirstname == '') {

The indented else if should just be if there is no preceeding if inside the block.

            else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
                     if (valuedeliveryfirstname == '') {

CodePudding user response:

First of all, you should not be using else if for all the other fields otherwise the if loop will exit when it enters the first matching condition. It should be:

            if(valueparticipantfirstname == '' ){
                alert('Participant firstname is required.');
                evt.preventDefault();
                $('#<%=TextBox_ParticipantFirstName.ClientID%>').focus();
            }
            if (valueparticipantlastname == ''){
                alert('Participant lastname is required.');
                evt.preventDefault();
                $('#<%=TextBox_ParticipantLastName.ClientID%>').focus();
            }
            if (valueOrganization == '') {
                alert('Organization is required.');
                evt.preventDefault();
                $('#<%=TextBox_Organization.ClientID%>').focus();
            }
            if (valueTeamName == 'Select Cheer Level') {
                alert('Select Cheer Level from the drop down list.');
                evt.preventDefault();
                $('#<%=DropDownList_CheerLevel.ClientID%>').focus();
            }
    .
    .
    .

Also, this code:

            else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
                     else if (valuedeliveryfirstname == '') {

needs to be changed to:

            else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
                     if (valuedeliveryfirstname == '') {
    .
    .
    .

  • Related