views:

45

answers:

2

Hello, I’ve spent the past hour search the web for possible solution, maybe I’m not using the right terms. I hope someone would be able to help me here. I have a form used inside the fancyBox iframe, after adding this ”if statement” below my submit button doesn’t work any more. However if I select more then 5 days I get the error alert (weird..). If I remove it the form submission works again.

if (VType == 'useRange'){
  var splitName = vacDays.split(" - ");
  var startDay =    Date.parse(splitName[0].trim());
  var endDay =  Date.parse(splitName[1].trim());
  var dayCount = (endDay - startDay) / (86400000);
  if (dayCount > 4)         
  alert('The date range cannot exceed 5 consecutive days');
  return false;
}

Here is the complete script, thank you in advance for you help!

$("#addVacButton").click(function(event){
  event.preventDefault();

  if (!$('#VacAddForm').valid())
  return false;

  var vacDays = $("#vacDays").val();
  var VacHours = $("input[name='VacHours']:checked").length;
  var VAC_UnschedTime = $("#VAC_UnschedTime").val();
  var VAC_Unsched = $("#VAC_Unsched").val();
  var VType = $("input[name='VTypeID']):checked").attr('id');

  //check if date range exceed 5 days
  if (VType == 'useRange'){
    var splitName = vacDays.split(" - ");
    var startDay =  Date.parse(splitName[0].trim());
    var endDay =    Date.parse(splitName[1].trim());
    var dayCount = (endDay - startDay) / (86400000);
    if (dayCount > 4)           
    alert('The date range cannot exceed 5 consecutive days');
    return false;
  }


  var data = $("#VacAddForm").serialize();
  $.ajax({  
    url: "cfc/fms.cfc?method=insertVAC&returnformat=plain",                  
    type: "POST",              
    data: data,                 
    cache: false,                
    success: function (returnMessage) {   
    var returnMsg = jQuery.trim(returnMessage);
      if (returnMsg.search(/Error/i) == '-1'){      
        alert('ok');
      }
      else{             
        alert('error');
      }  
    }
  });
  return false;

});

Here is the link:

<a id="VacAddForm" href="forms/VacAddForm.cfm?id=#id#"> Add</a> 
+1  A: 

You need to provide proper block for the if statement:

if (dayCount > 4) {           
        alert('The selected days can\'t exceed 5 days');
        return false;
}
Marimuthu Madasamy
Thanks Marimuthu, that was it. I was playing around wit the script and got it to work just as you've suggested.
A: 

On the 10th line, you should use // instead of \, hence your comment is wrong. Change it to backlash.

Newbie_25