- Home /
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.
Answer by The V Man · Sep 21, 2011 at 08:25 PM
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
Answer by by0log1c · Sep 21, 2011 at 08:36 PM
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 :)
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 overwhel$$anonymous$$g but the first time trying new code always is so I think I'll give it a shot. I appreciate the feedback!
Answer by gribbly · Sep 21, 2011 at 05:29 PM
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?
Would it be possible to omit letters a-z and only accept numbers 1-9 in the input field?
Your answer
Follow this Question
Related Questions
Taking data from text file 2 Answers
Text Prompt Problem 1 Answer
text input without ongui? 0 Answers
Avoid auto device capitalization for textfields on mobile 1 Answer
Display user Input to a text UI element. 0 Answers