Copy link to clipboard
Copied
I'm trying to add validation to my edittext controls. I need to make sure they are always numeric values. I've seen a number of posts indicating various methods for accomplishing this, but my edittext control is always returning an 'undefined' value regardless of whether I add a number or a letter. I can see the correct value in the debugger's view of the control, but the code fails. Can anyone please tell me what I'm doing wrong here?
CreateDialog.prototype.run = function()
{
// declare a bunch of vars
// Create a window of type palette.
var win = new Window("dialog", "Element Spray Generator",[iTop,iLeft,iTop + iWidth,iLeft + iHeight] ); // bounds = [left, top, right, bottom]
this.windowRef = win;
// add a bunch of other stuff...
win.txtEditScaleJitter = win.btnPanel.add("edittext",[win.btnPanel.bounds.width/5*4-iPadding, win.txtScaleJitter.bounds.top, win.btnPanel.bounds.width-iPadding, win.txtScaleJitter.bounds.bottom], "100%")
//Textbox events
win.txtEditScaleJitter.onChanging = function() {
//**** here win.txtEditScaleJitter.text is always undefined ******
var testCharacter = win.txtEditScaleJitter.text.charAt(win.txtEditScaleJitter.text.length -1);
switch(testCharacter ){
case "0": case "1": case "2": case "3":case "4": case "5":case "6":case "7":case "8": case "9": break;
default: alert("You entered a character that is not a number --> " + testCharacter + " <--"); break;
}
};
// Display the window
win.show();
return true;
}
Here is one example of checking for numeric entry.
var dlg=
"dialog{text:'Script Interface',bounds:[100,100,470,170],"+
"test:EditText{bounds:[10,10,360,30] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"button1:Button{bounds:[10,40,350,60] , text:'Cancel' }};"
var win = new Window(dlg,'test');
win.center();
win.test.onChanging = function() {
if (this.text.match(/[^\d]/)) {
this.text = this.text.replace(/[^\d]/g, '');
}
}
win.show();
Copy link to clipboard
Copied
Tom,
What I think we really need is a numeric input control or at least a creation property for edittext so it will only accept numbers. That way we wouldn't need keyboard handlers for this kind of thing.
Copy link to clipboard
Copied
Wanting to accept numbers can mean a lot of different things. Handling digits is relatively easy. When you have to deal with
floating point numbers, things get a bit gnarly because of localization issues. A decimal mark can be any of ".,-/" and mixing
in digit grouping separators just makes matters worse.
For what it's worth, I'm using this sort of code for keystroke filtering:
function filterKey(event) {
var obj = event.currentTarget;
var filter = obj.keyFilter; // a RegExp
var c = Number("0x" + event.keyIdentifier.substring(2));
var key = String.fromCharCode(c);
if (filter && key.match(filter)) {
return;
} else if (event.keyName.length == 1) {
// bail if it was a simple character that we couldn't match
event.stopPropagation();
event.preventDefault();
}
};
function addNumericFilter(obj) {
obj.keyFilter = /\d/;
obj.addEventListener('keydown', filterKey);
};
//...
addNumericFilter(pnl.textField);
It has the (dis)advantage of using RegExp for filtering, but it makes it easy to add new keystroke filters and gets around some of the issues of using onChanging.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now