lundi 18 mai 2015

using multiple nested if statement in javascript

Its my understanding that you can stack multiple nested if statements so that you can validate a users input on a form. I have tried to combine two nested if statements so that I can check a users input for multiple fields such as freight and mileage used when they submit but it only goes through the first nested if statement for mileage? Is there a better method to write a script to accomplish this kind of task? The code I have been working with is below. My goal is to have a message alert the user that they have not entered any mileage information for their out of town trip and/or have it alert them they included parts and ask if they ordered them to put the freight info in the freight field. if neither issue exists, I want it to drop out of the if statements and lock the form minus the customer's signature.

test = this.getField("City").value;
test1 = test.toUpperCase();

if (test1.indexOf("HOBBS") == -1) {
  if (this.getField("MileageTotal").value === 0) {
      app.alert("You have not listed any mileage for this trip.\n\n Please add mileage information for trips outside of the city.", 3, 0, "Trip Mileage Missing");
      app.alert(this.getField("MileageTotal").value);
  }
} else if (this.getField("TotalParts").value > 0 ){ 
  if(this.getField("Freight").value === 0) {
    result = app.alert("You have indicated that you have used parts on this job. \n Were any of these parts ordered?", 2, 2, "No Freight Entered");
  }
} else {
  for (var i = 0; i < this.numFields; i++){
      var fname = this.getNthFieldName(i);
      this.getField(fname).readonly = true;
  } // makes all fields readonly

  var f = this.getField("TechSigDate");
  f.value = util.printd("mmm/d/yyyy", new Date());
  this.getField("Customer Signature").readonly = false;
  this.getField("LockCustomer").readonly = false;
  this.getField("Lock Form").display = display.hidden;
} 

Aucun commentaire:

Enregistrer un commentaire