Skip to main content
Inspiring
October 28, 2009
Question

[JS][ScriptUI] Keeping invalid EditText active

  • October 28, 2009
  • 1 reply
  • 2866 views

Hi all,

I have a problem with validating EditText fields.

I got the onChange event handler right.

I got the routine that the textfield falls back to its previous value if the validating test is not met.

Where I fail is that I think the field should stay active as long as the user hasn't entered a valid value.

This is my code so far:

var win = new Window("dialog", "validate");

win.et1 = win.add("edittext", undefined, "0"); 

win.et2 = win.add("edittext", undefined, "1"); 

win.et1.characters = 10;

win.et2.characters = 10;

win.et1.fallback = "0";

win.et2.fallback = "1";

win.doneButton = win.add("button",undefined , "Close"); 

win.et1.onChange = check;

win.et2.onChange = check;

function check() { 

if (isNaN(Number(this.text))) {

alert("Type in a number");

this.text = this.fallback;

this.active = true;

}

this.fallback = this.text;

}; 

win.doneButton.onClick = function ()

win.close(); 

}; 

win.center(); 

win.et2.active = true;

win.show(); 

(also see enclosed zip)

The function "check" tries to set the fields "active" to true. But it seems that the function is called before the field is deactivated, so this doesn't do anything.

Is this possible?

A different approach I pondered is to not validate fields on change but when th OK button is clicked and then activate the appropriate field(s). But for this I would have to abort the OK-click.

I think this should be possible but I do not know how.

Thank you

Gerald

This topic has been closed for replies.

1 reply

Inspiring
October 28, 2009

BTW: what do I have to do to enter code in a proper formatting? If this has been discussed I have missed it

Loic.Aigon
Legend
October 28, 2009

Hi Gerald,

I swith onChange for onChanging and then it alerts you if the input is not a number and the field stay active.

Isn't that what you want ?

Loic

Inspiring
October 28, 2009

Hi Loic,

the number is just an example.

I actually need a multi-purpose field that in some cases is validated against a grep. In those cases the check makes sense only when the entry is finished, i.e. onChange and not onChanging.

As I said, I've got everything working but that one problem of the cursor being in the wrong field after onChange.

If there is any way to keep the field active from inside the onChange handler, the solution would be easy.

Otherwise I suppose I have to rewrite everything to check on onClick on the OK-button.