JS script to validate date in between 2 dates gives error if end date is different month

I’m trying to develop a script for Flokzu (A BPMN software that uses js scripts to define form field validations) and i’m asked to validate vacation days request fields. E.g. start date is after today and before end date. So far it works as long as the end date is within the same month; as soon as the end date is a different month, the if block kicks in. This is the code i’ve come up with so far, with corresponding translation:

  • [[Fecha de salida]] = Departure Date (field filled in the form from a calendar dropdown and grabbed by the script)
  • [[Fecha de regreso]] = Return Date (field filled in the form from a calendar dropdown and grabbed by the script)
  • Flokzu.onChange() = platform specific function that executes the function when the listened field changes its value in the form
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function Fechas(accion){
if(accion){
return moment(Flokzu.getFieldValue( [[Fecha de salida]]),
"YYYY/MM/DD")
}else{
return moment(Flokzu.getFieldValue( [[Fecha de regreso]]),
"YYYY/MM/DD")
}
}
function ValidarFechas(msg, boton) {
var Salida= Fechas(true), Regreso= Fechas(false), today = moment();
// Check if Salida is before today or after Regreso
if (Salida.isSameOrBefore(today) || Salida.isAfter(Regreso)) {
swal({
type: 'error',
title: 'Error en las fechas!',
text: 'La fecha de salida debe ser posterior al día de hoy y anterior o igual a la fecha de regreso'
});
Flokzu.error([[Fecha de salida]], "Cambie a un rango de fechas valido");
}
}
Flokzu.onChange([[Fecha de regreso]], ValidarFechas);
</code>
<code>function Fechas(accion){ if(accion){ return moment(Flokzu.getFieldValue( [[Fecha de salida]]), "YYYY/MM/DD") }else{ return moment(Flokzu.getFieldValue( [[Fecha de regreso]]), "YYYY/MM/DD") } } function ValidarFechas(msg, boton) { var Salida= Fechas(true), Regreso= Fechas(false), today = moment(); // Check if Salida is before today or after Regreso if (Salida.isSameOrBefore(today) || Salida.isAfter(Regreso)) { swal({ type: 'error', title: 'Error en las fechas!', text: 'La fecha de salida debe ser posterior al día de hoy y anterior o igual a la fecha de regreso' }); Flokzu.error([[Fecha de salida]], "Cambie a un rango de fechas valido"); } } Flokzu.onChange([[Fecha de regreso]], ValidarFechas); </code>
function Fechas(accion){
   if(accion){ 
    return moment(Flokzu.getFieldValue( [[Fecha de salida]]), 
            "YYYY/MM/DD")
  }else{
    return moment(Flokzu.getFieldValue( [[Fecha de regreso]]), 
            "YYYY/MM/DD")
 }
}
function ValidarFechas(msg, boton) {
    var Salida= Fechas(true), Regreso= Fechas(false), today = moment();

    // Check if Salida is before today or after Regreso
    if (Salida.isSameOrBefore(today) || Salida.isAfter(Regreso)) {
        swal({
            type: 'error',
            title: 'Error en las fechas!',
            text: 'La fecha de salida debe ser posterior al día de hoy y anterior o igual a la fecha de regreso'
        });
        Flokzu.error([[Fecha de salida]], "Cambie a un rango de fechas valido");
    }
}
Flokzu.onChange([[Fecha de regreso]], ValidarFechas);

If, for example, I evaluate with values:

  • Departure = 2024/05/27
  • Return = 2024/05/31
  • Today = 2024/05/26 (selected automatically from the “moment()” function

It works as intended

But if i select

  • Departure = 2024/05/27
  • Return = 2024/06/01
  • Today = 2024/05/26 (selected automatically from the “moment()” function

It displays the error.

Any idea why? How could i solve this?

Unfortunately I cannot log form outputs since the implementation of scripts is kind of raw and only lets me write the script in a pop up text field and save it. Then I need to run the process and simulate some form fill outs to check for errors so I’m not sure if I’m grabbing the dates in a correct format. Thanks in advance

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật