Validating Data in ScriptUI
I was referred to @Peter Kahrel excellent tutorial on ScriptUI located here https://creativepro.com/files/kahrel/indesign/scriptui.html, but I am having a few issues with my scripts:
- On page 93, he has an example with this code:
w.input = w.group.add ('edittext {characters: 10, active: true, justify: "right"}');I don't see where "characters: 10" is explained. I thought it would limit the input to 10 characters, but it does not seem to, either in his example or in my testing. Is there a way to do this? One of my input fields needs to be 7 characters and one of them needs to be 8 character date in format YYYYMMDD.
- For the field that needs to be 7 characters, I use an Apply instead of an Okay button and I used his red-background code (and modified it to NOT allow commas or decimals). But I don't want the apply button enabled unless the field has seven characters - not 8 or 6. I tried this and it doesn't work:
win.Panel2.txt2.onChanging = function () {
var valid = /^[\d]+$/.test (win.Panel2.txt2.text);
this.graphics.backgroundColor = this.graphics.newBrush (this.graphics.BrushType.
SOLID_COLOR, valid ? [1, 1, 1, 1] : [1, 0.5, 0.5, 1]);
win.Panel2.ApplyBtn.enabled = valid && win.Panel2.txt2.text.length=7;
}
- Similarly for the date field, I found this: https://www.scaler.com/topics/date-validation-in-javascript/ and I verified Date.parse() generates NaN for an invalid date in the format above and a number for a valid date. I want the apply button to be enabled only if I have a valid date in the field, and I want the red text if the field contains anything besides numbers. I tried this and it doesn't work - the Apply button is never enabled:
win.Panel2.txt3.onChanging = function () {
var valid = /^[\d]+$/.test (win.Panel2.txt3.text);
this.graphics.backgroundColor = this.graphics.newBrush (this.graphics.BrushType.
SOLID_COLOR, valid ? [1, 1, 1, 1] : [1, 0.5, 0.5, 1]);
var valid2 = Date.parse(win.Panel2.txt3.text);
win.Panel2.ApplyBtn.enabled = !valid2==NaN;
}
I'll probably have more questions as I get further into my script, and I'll add them to this thread, if that is okay.
(The forum gave me an error about invalid HTML that it removed, but I'm not seeing what it changed).
Thanks in advance!
