| Posted: 08 May 2009 17:53 Last Edited By: admin | |
|
Registered User |
Posts: 12 Join Date: May 2009 |
| Whats the best way to create a numeric only textedit using ScriptUI. When using InDesign's built-in dialog object this can be done easily, how is this done with ScriptUI. | |
| Posted: 08 May 2009 17:57 Last Edited By: admin | |
|
Administrator |
Posts: 16 Join Date: May 2009 |
|
Here is a code sample using the textedit's onChanging event
Code:
var rapidDlg = new Window('dialog',"Numeric Only",undefined); rapidDlg.EditText1 = rapidDlg.add('edittext',undefined,"0"); rapidDlg.EditText1.characters = 4; rapidDlg.EditText1.onChanging = numbersOnly; rapidDlg.show(); function numbersOnly(){ if (numbersOnly.staticNumber==undefined){numbersOnly.staticNumber='0'} if(!isNaN(+ this.text)){numbersOnly.staticNumber=this.text} else{ this.text=''; //removes the new text this.textselection=numbersOnly.staticNumber //adds the old text with selection at end of written text }; } The trick here is to create a static variable, which is done in javascript by creating variable as part of function. One thing about this code is that it will accept decimal values. |
|
| Posted: 11 May 2009 18:26 Last Edited By: admin | |
|
Registered User Currently Offline |
Posts: 2 Join Date: May 2009 |
|
This is ripped from xtools/xlib/GenericUI but shows a couple of different ways of filtering text entry widgets.
Code:
Filters = {}; // check to see if this contains only valid floating point characters Filters.numberKeystrokeFilter = function() { if (this.text.match(/[^\-\.\d]/)) { this.text = this.text.replace(/[^\-\.\d]/g, ''); } }; // check for only numbers Filters.numericKeystrokeFilter = function() { if (this.text.match(/[^\d]/)) { this.text = this.text.replace(/[^\d]/g, ''); } }; // check for something that looks like a UnitValue Filters.unitValueKeystrokeFilter = function() { if (this.text.match(/[^a-z0-9% \.]/)) { this.text = this.text.toLowerCase().replace(/[^a-z0-9% \.]/g, ''); } }; // Example: only accept numbers rapidDlg.EditText1.onChanging = Filters.numericKeystrokeFilter; |
|
| Posted: 14 May 2009 04:26 | |
|
Administrator |
Posts: 16 Join Date: May 2009 |
|
Thanks xbytor for your post.
I learned from your post that the 'this' keyword will get me the caller of the function, and fixed that in my code. I also made another change to my code. The reason being that anytime a user types an illegal character the cursor moves back to the beginning of the edittext. So first I cleared the textedit and then using textselection property I put in the legal text which leaves the cursor at the end of the text. It's still not perfect, since at times the user will add illegal text in the middle of the textedit and the cursor will jump to the end. Over all a big advantage of the your xtools library way, is the ability to use the same function for more then one textedit, which I can only do with workarounds since the static variable will only hold one value. Thanks P.S. I downloaded your xlibs library and I hope to see what else I can learn from there. |
|
| Posted: 15 May 2009 22:21 | |
|
Registered User Currently Offline |
Posts: 2 Join Date: May 2009 |
|
Do something more like to make the numbersOnly function universally reusable.
Code:
function numbersOnly(){ if (this.staticNumber==undefined) { this.staticNumber='0'; } if(!isNaN(+ this.text)) { this.staticNumber = this.text; } else { this.text=''; //removes the new text this.textselection = this.staticNumber; //adds the old text with selection at end of written text } }; BTW, I generally use: Code:
function toNumber(s) { if (s == undefined) { return NaN; } if (s.constructor == String && s.length == 0) { return NaN; } if (s.constructor == Number) { return s.valueOf(); } return Number(s.toString()); }; to convert strings to numbers. Makes life easier. |
|