new expiry date must be a quarter end dates(mar 31, june 30, sep 30, dec 31)

test228
Tera Contributor

new expiry date must be a quarter end dates(mar 31, june 30, sep 30, dec 31) need a date validation on the catalog item variable

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@test228 

something like this in onChange client script

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }
  // Parse the selected date
  var date = new Date(newValue);
  var month = date.getMonth() + 1; // Months are 0-based in JS
  var day = date.getDate();

  // Check if the date is a valid quarter-end
  var isQuarterEnd = (
    (month === 3 && day === 31) ||
    (month === 6 && day === 30) ||
    (month === 9 && day === 30) ||
    (month === 12 && day === 31)
  );

  if (!isQuarterEnd) {
    g_form.showFieldMsg('expiry_date', 'Expiry date must be a quarter end (Mar 31, Jun 30, Sep 30, Dec 31)', 'error');
    g_form.clearValue('expiry_date');
  } else {
    g_form.hideFieldMsg('expiry_date');
  }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@test228 

something like this in onChange client script

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }
  // Parse the selected date
  var date = new Date(newValue);
  var month = date.getMonth() + 1; // Months are 0-based in JS
  var day = date.getDate();

  // Check if the date is a valid quarter-end
  var isQuarterEnd = (
    (month === 3 && day === 31) ||
    (month === 6 && day === 30) ||
    (month === 9 && day === 30) ||
    (month === 12 && day === 31)
  );

  if (!isQuarterEnd) {
    g_form.showFieldMsg('expiry_date', 'Expiry date must be a quarter end (Mar 31, Jun 30, Sep 30, Dec 31)', 'error');
    g_form.clearValue('expiry_date');
  } else {
    g_form.hideFieldMsg('expiry_date');
  }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader