Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 MrThreepwood · Jan 11, 2013 at 01:57 AM · guitextfieldparse

How to Get a Textfield to Accept only numbers and periods.

Currently I'm using this section of code:

 if (chr < '0' || chr > '9' && chr != '.') {
    Event.current.character = '\0';
 }

However, I've come to realize that I can't place a period into the text field with or without this code. I can't use commas either. Long story short I'm getting frustrated not being able to place/display float values in these fields.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Jan 11, 2013 at 02:02 AM

Something like this: http://answers.unity3d.com/questions/40103/text-field--inputting-numbers-only.html

Comment
Add comment · Show 3 · 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 MrThreepwood · Jan 11, 2013 at 02:09 AM 0
Share

As elegant as that may seem, that won't allow me to place periods in the text field. I would like players to be able to select a value for a given item, and it would automatically adjust the cost, however, I would like them to be able to use decimals. Currently I can't find a way to allow periods to be input into the textfield.

avatar image Lovrenc · Jan 11, 2013 at 03:02 AM 0
Share

This is great link to beautiful example and deserves appreciation. All you have to do then is

a) Learn and understand the code b) Tweek it to your needs.

And this is what this portal is all about. Helping people to learn and steer them into right direction. And then when you gain you should help others on their stuff. Just expecting complete scripts and then pasting (and not even pasting in wright place) does nothing.

Bonus: All you have to do is use float.tryParse if you want floating points.

avatar image Eric5h5 · Jan 11, 2013 at 05:05 AM 0
Share

As I mentioned in my comment on that answer, the code won't actually work, so it's not really a good example. See my answer here for the best way I've found so far (the Regex example will also work, but has usability issues as described by $$anonymous$$olix in the comment on my answer).

avatar image
0

Answer by Eric5h5 · Jan 11, 2013 at 05:00 AM

It's just a matter of fixing the logic by using parentheses, since the expression is evaluated left to right, and elements inside parentheses are grouped:

 if ( (chr < '0' || chr > '9') && chr != '.') {
      Event.current.character = '\0';
 }

However that would still allow multiple periods, which is probably not desirable, so that could be fixed (assuming that your string variable is called "myString"):

 if ( ((chr < '0' || chr > '9') && chr != '.') || (chr == '.' && myString.Contains(".")) ) {
     Event.current.character = '\0';
 }
Comment
Add comment · Show 2 · 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 MrThreepwood · Jan 11, 2013 at 09:29 PM 0
Share

As mentioned previously, even without that section of code (nice catch by the way) I can't put periods inside a text field. Just generic textfield with no intercept code, still can't type a period there.

avatar image Eric5h5 · Jan 11, 2013 at 09:46 PM 0
Share

Some kind of localization/keyboard problem maybe?

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.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with Android Keyboard/TextField 1 Answer

Background of GUI.TextField disappears when using a GUIStyle 2 Answers

Select all text in a GUI.TextField? 1 Answer

GUI.TextField problem, won't update var. 0 Answers

gui text and password field 1 Answer


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