# Converting keypress into a string of only numbers

**URL:** <https://forum.kirupa.com/t/converting-keypress-into-a-string-of-only-numbers/327453>\
**Category:** flash\
**Created:** [March 1, 2012, 8:43pm UTC](https://forum.kirupa.com/t/converting-keypress-into-a-string-of-only-numbers/327453 "2012-03-01T20:43:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![xCiter](https://avatars.discourse-cdn.com/v4/letter/x/9e8a1a/32.png) [@xCiter](https://forum.kirupa.com/u/xCiter)\
**Post date:** [March 1, 2012, 8:43pm UTC](https://forum.kirupa.com/t/converting-keypress-into-a-string-of-only-numbers/327453/1 "2012-03-01T20:43:52Z")

</div>

Ok so after solving my last problem, i have a new one which i’ve tried to work on but have not yet succeeded ☹

What i want to do is to allow a user to input a time of when the alarm should sound. However, when i press like “2” or any number i get “22” instead of just “2”

Also, is there a way to stop people from entering letters along with this piece of code?

Here is how i attempted to do it:

```auto
var alarmTime:TextField = new TextField();
var textAlarmFormat:TextFormat = new TextFormat();
textAlarmFormat.align = TextFormatAlign.CENTER;
textAlarmFormat
addChild(alarmTime);
alarmTime.x = 100;
alarmTime.y = 6;
alarmTime.type = TextFieldType.INPUT;
alarmTime.defaultTextFormat = textAlarmFormat;

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkTextInput);

function checkTextInput(e:KeyboardEvent){
    
    var character:String = String.fromCharCode(e.charCode);
    
    if (e.keyCode >=48 && e.keyCode <= 57)
    {
        alarmTime.text = character;
    }
    
}

```
