x


comparing a number input to a letter input

Hello, I'm trying to write a script that only accepts numbers typed from the keyboard and not numbers in a text field. Is this possible with unit's javascript. any help is awesome.

more ▼

asked Sep 21 '11 at 04:52 PM

ThumbStorm gravatar image

ThumbStorm
418 27 33 42

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

I think you're looking for something like this: http://forum.unity3d.com/threads/12341-Validating-user-input

Converting that from C# to UnityScript should be fairly straight-forward.

-V

more ▼

answered Sep 21 '11 at 08:25 PM

The V Man gravatar image

The V Man
16 2 2 4

Thank you, very helpful.

Sep 22 '11 at 02:24 PM ThumbStorm
(comments are locked)
10|3000 characters needed characters left

While it seem overwhelming at first, Regular Expressions can do all sort of complex string manipulations. You can allow/unallow any kind of 'expressions/pattern' from a string. You'll find lot of documentation and even lots of prepared RegularExpression out there.

OR you could manually validate it by comparing each input's character to a AllowedCharacters char array, or something similar.

OR you could use string.Replace("a",""); but this is tedious and will leave tons of unexpected characters.

Best bet is the Regex :)

more ▼

answered Sep 21 '11 at 08:36 PM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

I did start validating using a char array and it's working well even though I'm sure it's the long work around. The regular expressions does seem overwhelming but the first time trying new code always is so I think I'll give it a shot. I appreciate the feedback!

Sep 22 '11 at 02:27 PM ThumbStorm
(comments are locked)
10|3000 characters needed characters left

It's definitely possible with JavaScript.

Sounds like you don't want to show a TextField, you just want to capture numbers. Can you just do something like this ?

var userInput:int;

if(input.GetKeyDown("1")){
    userInput = 1;
}
if(input.GetKeyDown("2")){
    userInput = 2;
}
//and so on...

...or am I misinterpreting your question?

more ▼

answered Sep 21 '11 at 05:29 PM

gribbly gravatar image

gribbly
390 1 2 5

Would it be possible to omit letters a-z and only accept numbers 1-9 in the input field?

Sep 21 '11 at 07:27 PM ThumbStorm
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x954
x553
x151

asked: Sep 21 '11 at 04:52 PM

Seen: 826 times

Last Updated: Sep 22 '11 at 02:27 PM