How do I check if a string contains only numbers?

MAXScript Frequently Asked Questions

A user asked:

I like to check whether the text of an edittext UI element contains numbers or letters.

If it contains numbers, I like it to return the number.

But if it contains letters, I want it to display a message "You may only type numbers, try again"

How do I do that ?

Answer:

Script

rollout MicroListener "MicroListener"

(

edittext myEditText text:""

label theLabel "Ready."

on myEditText changed txt do

(

if txt != "" then

(

try

(

val = execute txt

theLabel.text = "You typed in " + (val as string )

)

catch  

theLabel.text = "Invalid Expression!"

)

else

theLabel.text = "Ready."

)

on myEditText entered txt do myEditText.text = ""

)

createDialog MicroListener 300 60

)

 

This simple script implements a kind of "micro-listener". It uses the Execute method to turn the string typed in the edittext field into a MAXScript expression. It lets you not only enter numbers, but also constants like Pi, or global variables that you have defined. Not only this, you can define global variables inside the script, and even create objects on the fly!

Here are some examples

microListener_125_1.gif

125.1 is a valid expression - it is a constant and evaluates to itself (125.1).

 

microListener_d.gif

On the other hand, d returns undefined as it is undefined.

 

microListener_e.gif

Surprisingly, e does not return undefined - it is a reserved keyword in MAXScript!

 

microListener_pi.gif

Same with pi - as expected, it returns 3.14149....

 

microListener_a_b.gif

Trying to add two undefined values results in an invalid expression.

 

microListener_bobo.gif

This variable is also undefined...

 

microListener_bobo_10.gif

...Until we define it! Now it is equal to 10...

 

microListener_2boboPi.gif

...And so we can write an expression containing constants and variables!

 

microListener_box_.gif

Typing in a function name, in this case the constructor of a box, we get the function back.

 

microListener_box__.gif

By calling the function, we can create objects just like in the "real" Listener!

 

microListener_box_100.gif

This box will be created 100 units to the right because we specified the position...

And so on.

 

See also

Execute

Frequently Asked Questions