• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ThumbStorm · Sep 21, 2011 at 04:52 PM · inputtexttextfield

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThumbStorm · Sep 22, 2011 at 02:24 PM 0
Share

Thank you, very helpful.

avatar image
1

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThumbStorm · Sep 22, 2011 at 02:27 PM 0
Share

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!

avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThumbStorm · Sep 21, 2011 at 07:27 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Taking data from text file 2 Answers

Text Prompt Problem 1 Answer

text input without ongui? 0 Answers

Text.Field erratically updating 0 Answers

Display user Input to a text UI element. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges