timchen
timchen

Reputation: 388

how to validate date using client handler?

I'm wondering if there's a way to validate date from DatePicker in client side? I would like to compare the date from DatePicker with now date.

I see it seems only have validateLength(), validateRange(), etc.

But those seems not work for date.

Upvotes: 0

Views: 841

Answers (2)

Bryan P
Bryan P

Reputation: 5051

An addValueChangeHandler would work...

function doGet() {
  var app = UiApp.createApplication();
  var date = app.createDateBox().setId('date').setName('date');
  var label = app.createLabel('').setId('status');
  var handler = app.createServerHandler('checkDate').addCallbackElement(date);
  date.addValueChangeHandler(handler)
  app.add(date).add(label);
  return app;
}

function checkDate(e) {
  var app = UiApp.getActiveApplication();
  var date = e.parameter.date;
  var today = new Date().getTime();
  if (date.getTime() < today)
    app.getElementById('status').setText('Date is before today.');
  else app.getElementById('status').setText('Date is after today.');
  return app;
}

Try it.

Upvotes: 0

DDD
DDD

Reputation: 1462

Afraid not. Either use server handlers or you write your UI in HtmlService so you can code your own client side validation without the client handler restrictions. Coding your own ui shouldn't be too much trouble, especially if you use something like jQuery UI for your widgets - here's an example.

Upvotes: 1

Related Questions