Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>package Utility { import flash.events.Event; import flashx.textLayout.operations.PasteOperation; import spark.components.TextInput; import spark.events.TextOperationEvent; /** * This is a text input component that can only input numbers. * A number means a real number. * @author Cheng Liang * @version 0.0.1 */ public class NumberInput extends TextInput { [Bindable("numberChange")] /** * Sets number as input value. */ public function set number(value : Number) : void { var s : String = value.toString(); if(text != s) { text = s; dispatchEvent(new Event("numberChange")); } } public function get number() : Number { return Number(text); } public function NumberInput() { super(); this.addEventListener(TextOperationEvent.CHANGE, onTextChange); } protected function onTextChange(event : TextOperationEvent) : void { if (event.operation is PasteOperation) { TextInput(event.currentTarget).text=''; } if(text == "" || text.length == 1) { return; } /* To allow single (.) */ var idx : int = text.indexOf(".", 0); if(idx &gt;= 0) { text = text.substring(0, idx + 1) + text.substring(idx + 1).replace(".", ""); } /* To allow single (-) only at the 1st position*/ if(text.lastIndexOf("-")==0) text ='-'+ text.substring(1); else if(text.lastIndexOf("-")&gt;0) { if(text.charAt(0)=='-') text ='-'+ text.substring(1).replace("-", ""); else text =text.replace("-", ""); } var arr:Array = TextInput(event.currentTarget).text.split("."); try{ if(arr[1] &amp;&amp; String(arr[1]).length &gt; 2){ TextInput(event.currentTarget).text = arr[0]+"."+String(arr[1]).slice(0,2); } }catch(err:Error){} this.selectRange((text.length), (text.length)); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload