Welcome Guest, Not a member yet? Register   Sign In
Help with variables

Hi everyone,

I have a view that has a form that allows a user to enter guest details. They submit the form and it needs to check to see if they have entered in all the fields. The problem I am having is that it is only evaluating the first guests details and not the rest. What I want to do is save all the text boxes in an array and then check to see if each one is empty, if it is, then I want to alert them. It doesn't seem to be working.

Here is my code:

This dynamically displays the amount of guests input boxes depending on the input of how many they want.

                                    var mycounter = 1;
                                    var limit = <? echo $limitvar; ?>;
                    var allIDname = [];
                    var allIDemail = [];
                    var allIDphone = [];

                    function addInput(number){
                        mycounter = 1;
                        document.getElementById("dynamicInput")[removed] = "";
                        var num = parseInt(number);
                        var i;

                        for (i = 0 ;i <num; i++){
                            if (mycounter == limit)  {
                                alert("You have reached the limit of adding " + mycounter + " inputs");
                            var newdiv = document.createElement('div');
                            newdiv[removed] = "   <tr><td>Guest " + mycounter + "<td></tr> <table><tr><td>Guest name</td><td>Guest email</td><td>Guest phone number</td></tr> <tr><td>&lt;input type='text' name='myGuestName[]' id='agname'&gt;&lt;/td><td>&lt;input type='text' name='myGuestEmail[]' id='agemail'&gt;&lt;/td><td>&lt;input type='text' name='myGuestPhone[]' id='agphone'&gt;&lt;/td></tr></table>";
                            // Adding the input boxes to an array
                            allIDname = document.getElementById('agname');
                            allIDemail = document.getElementById('agemail');
                            allIDphone = document.getElementById('agphone');

                    // Supposed to validate to see if there is any input in all the boxes when form is submitted
                    function validateGuest() {
                        var guestName = document.getElementById('agname');
                        var guestEmail = document.getElementById('agemail');
                        var guestPhone = document.getElementById('agphone');
                        // only alerting first guest's details
                        if (guestName.value == "") {
                            alert("Please enter the guest's name");
                            return false;
                        } else if (guestEmail.value == "") {
                            alert("Please enter the guest's email");
                            return false;
                        } else if (guestPhone.value == "") {
                            alert("Please enter the guest's phone");
                            return false;

                        return true;

Theme © iAndrew 2016 - Forum software by © MyBB