Elena Calvillo
Elena Calvillo

Reputation: 31

JavaScript code equivalent in ActionScript

Hi everyone I'm new in both languages... I found a example for scrambled numbers in JavaScript and HTML I would like to convert that code into ActionScript code, the languages looks similar for me but when I compile it in Flash Builder nothing happens... I would like to understand how to do the example but in ActionScript and MXML code...

JavaScript code parts that I don't understand how to do it in ActionScript:

function check(value)
{
    if ( value != Math.round(value) )
   alert("You must enter an integer in this input box.");
}
function generate( )
{
var minval = parseInt(**document.form.min.value**);
    if ( isNaN(minval) || minval != parseFloat(**document.form.min.value**))

And To implement this with TextInput in HTML:

<INPUT TYPE="text" NAME="**min**" VALUE="" ONCHANGE="check(**this.value**)">

I don't know hot to leave it in AS3 and MXML... I tried:

public function generate()
{
var minval = parseInt(**min**);
if ( isNaN(minval) || minval != parseFloat(**min**))
{

and the MXML:

<s:TextInput id="**min**" change="**check(this)**" />

As you can see I'm very lost...

Upvotes: 1

Views: 431

Answers (2)

Sakthi Kumar
Sakthi Kumar

Reputation: 3045

<fx:Script>

<![CDATA[

import flash.events.Event;
import mx.controls.Alert;
public function check(event:Event):void
{               
        if (isNaN(parseInt(min.text)) || isNaN(parseFloat(min.text))) {
            /* Do something here*/
            Alert.show("Input a number", "Error");
        }               
}
]]>
</fx:Script>
<s:TextInput id="min" change="check(event)"/>

This code works the way you want I guess. Please bear something in mind that the parseInt function ignores any trailing non numeric characters after a valid integer. Say for example, the code works if you enter "a" first, but fails if u enter 1a. More details about the function can be found here

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/package.html#parseInt()

Upvotes: 1

RobG
RobG

Reputation: 147523

Just a comment:

The value of an input element is always returned as a string, so to check if the user has entered an integer, you should just see if the value is all digits, e.g.:

    var isInt = /^\d+$/.test(s); // '123' -> true, '123a' -> false

You may want to trim leading and trailing spaces first, or not.

Upvotes: 1

Related Questions