jampez77
jampez77

Reputation: 5241

how can i execute a function once the end of an array is reached?

I have the following code...

 var min_val = [{"id":"driverDOA","value":date_of_accident},etc etc...];//put validation elements in an array

       for (var i=0;i<min_val.length;i++)//loop through each element if array
        {
            var object = min_val[i];
            if(object.value == "" || object.value == null)//if element is null or empty then execute code
                {
                    document.getElementById(object.id).parentNode.innerHTML += '<div class="prompt accident">*</div>';
                    document.getElementById('accident-tab').innerHTML += '<div class="prompt accident">*</div>';
                    return false;
                }
            else
                {
                    $('.prompt accident').remove();
                                            sendAjaxRequest();
                }
        }

basically,I'm validating a form by storing the elements required in an array. I then loop through the array and check for any empty values. If the user has left a value then an asterisk is shown next to that element and on the tab for that section.

Once the user has filled in all of the marked form elements then the code removes the asterisks and send an Ajax request.

However at the moment once an empty element has been found it displays the asterisk for that and stops.

What i need is for it to loop through the entire array each time and display all asterisks. I have a feeling that this will be remarkably simple but have been unable to find anything or figure it out.

Any help would be appreciated

Upvotes: 0

Views: 106

Answers (3)

mplungjan
mplungjan

Reputation: 178350

Here

Change return false to noerror=false, add var noerror=true to before the loop and end the function with return noerror

Like this

var noerror=false, min_val = [{"id":"driverDOA","value":date_of_accident},etc etc...];//put validation elements in an array
$('.prompt accident').remove(); // reset
for (var i=0;i<min_val.length;i++) { //loop through each element if array
  var object = min_val[i];
  if(object.value == "" || object.value == null) { //if element is null or empty then execute code
    document.getElementById(object.id).parentNode.innerHTML += '<div class="prompt accident">*</div>';
    document.getElementById('accident-tab').innerHTML += '<div class="prompt accident">*</div>';
    noerror=false;
  }
 }
 if (noerror) {              
  sendAjaxRequest();
 }
 return noerror;
}

Upvotes: 1

Zafer
Zafer

Reputation: 2190

var hasError=false;
for (var i=0;i<min_val.length;i++)//loop through each element if array
        {
            var object = min_val[i];
            if(object.value == "" || object.value == null)//if element is null or empty then execute code
                {
                    document.getElementById(object.id).parentNode.innerHTML += '<div class="prompt accident">*</div>';
                    document.getElementById('accident-tab').innerHTML += '<div class="prompt accident">*</div>';
                    hasError= true;
                    //return false; // remove this...
                } else $('.prompt accident').remove();
        }
  if(!hasError) {
       sendAjaxRequest();
  }

Upvotes: 2

Aadit M Shah
Aadit M Shah

Reputation: 74234

Don't return false. Just continue.

Edit: A better solution would be:

 var min_val = [{"id":"driverDOA","value":date_of_accident},etc etc...];//put validation elements in an array
       var valid = true; // I MADE A CHANGE HERE
       for (var i=0;i<min_val.length;i++)//loop through each element if array
        {
            var object = min_val[i];
            if(object.value == "" || object.value == null)//if element is null or empty then execute code
                {
                    document.getElementById(object.id).parentNode.innerHTML += '<div class="prompt accident">*</div>';
                    document.getElementById('accident-tab').innerHTML += '<div class="prompt accident">*</div>';
                    valid = false; // AND HERE
                }
            else
                {
                    $('.prompt accident').remove();
                                            sendAjaxRequest();
                }
        }
        return valid; // AND HERE

Upvotes: 0

Related Questions