Matthew Denaburg
Matthew Denaburg

Reputation: 165

Google UI Script validateMatches doesn't work, validateNotMatches does

I am designing a form using Google Scripting. I have two text boxes that need to hold time values, and I want to make sure the input is valid.

function setupTimeValidators(widget) {
  var timeRe = /(0[1-9])|(1[0-2]):[0-5][0-9] ?[ap]m/i;

  var onValid = (UiApp.getActiveApplication().createClientHandler()
                 .validateMatches(widget, timeRe)
                 .forTargets(widget)
                 .setStyleAttribute("background", "#FFFFFF"));


  var onInvalid = (UiApp.getActiveApplication().createClientHandler()
                   .validateNotMatches(widget, timeRe)
                   .forTargets(widget)
                   .setStyleAttribute("background", "#FFCCCC"));

  widget.addKeyUpHandler(onValid);
  widget.addKeyUpHandler(onInvalid);
}

The onInvalid parts changes the textbox background color as soon as I start typing in the textbox, but it never changes back to white when I get to 01:11 pm. (I have tested this with other values.)

I am sure my regular expression works, because I tested it like so:

function test() {
  Browser.msgBox(/(0[1-9])|(1[0-2]):[0-5][0-9] ?[ap]m/i.test("01:11 pm")); // true
  Browser.msgBox(/(0[1-9])|(1[0-2]):[0-5][0-9] ?[ap]m/i.test("00:11 pm")); // false
  Browser.msgBox(/(0[1-9])|(1[0-2]):[0-5][0-9] ?[ap]m/i.test("10:11 pm")); // true
  Browser.msgBox(/(0[1-9])|(1[0-2]):[0-5][0-9] ?[ap]m/i.test("10:90 pm")); // false
}

Any ideas what could be going on? Thanks!

Upvotes: 0

Views: 267

Answers (1)

Corey G
Corey G

Reputation: 7858

You can't use a regex object with validateMatches or validateNotMatches.. you should use a string representation of the regex, as such:

var timeRe = "(0[1-9])|(1[0-2]):[0-5][0-9] ?[ap]m";
var flags = "i";

...

.validateMatches(widget, timeRe, flags)

Upvotes: 1

Related Questions