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 Kalu · Dec 08, 2011 at 12:03 AM · guitextfieldregex

Allow only numbers in an input field

I want to know how to restrict the input field numbers and just ignore the letters, I found it but the method of "int" does not exist, or rather "int" does not exist. I Program in C #

 GUI.Label(new Rect(positionX, positionY, _W, _H), "Epaisseur (mm):");
                 stringToEditH = GUI.TextField (new Rect (positionX, positionY+_H, _W/3f, 20f), num.ToString(), 3);
                 
                 int temp = 0;
                 if (int.TryParse(stringToEditH, temp))
                 {
                     num = Mathf.Clamp(0, temp);
                 }
                 else if (text == "") num = 0;
Comment
Add comment · Show 3
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 Kalu · Dec 08, 2011 at 12:07 AM 0
Share

I would also like to know if it is possible to edit the value of the textfield at click on an object, put the value of the field.

avatar image Kalu · Dec 08, 2011 at 12:19 AM 0
Share

Assets/Standard Assets/Scripts/Utility Scripts/$$anonymous$$ain.cs(271,37): error CS1502: The best overloaded method match for `int.TryParse(string, out int)' has some invalid arguments

//(my "stringToEditH" is declared as a string)

this is the last error i get. Time to sleep :) thx

avatar image jahroy · Dec 08, 2011 at 12:21 AM 0
Share

You will get that error if you try to call int.TryParse on a string that doesn't represent a valid integer.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jahroy · Dec 08, 2011 at 12:20 AM

It's very easy if you want to use a regex:

I don't usually use C#, but a quick look at the documentation makes me think it would look like this:

/* define a pattern: everything but 0-9 */

Regex rgx = new Regex("[^0-9]");

/ replace all non-numeric characters with an empty string /

stringToEditH = rgx.Replace(stringToEditH, "");

 

You might have to fiddle with it, but that's the idea....

------------

If you don't want to use a regex, you could do it yourself like this:

function stripNonNumerics ( inputString : String ) : String { var theResult : String = "";

 for ( var i = 0; i < inputString.length; i ++ ) {

     var theString : String = inputString.Substring(i, 1);
     var tempInt   : int;

     if ( int.TryParse(theString, tempInt) ) {
         theResult += theString;
     }
 }

 return theResult;

}

 

There are probably a few better ways to do it, but that seems to work for me.

That's in UnityScript... it will probably be easier in C# where you can use chars!!

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 Kalu · Dec 08, 2011 at 12:24 AM 0
Share

The type or namespace name `Regex' could not be found. :s

avatar image Kalu · Dec 08, 2011 at 12:31 AM 0
Share

using System.Text.RegularExpressions;

Perfect! Thx.

avatar image jahroy · Dec 08, 2011 at 12:32 AM 0
Share

You have to add the Regex library if you want to use its functions.

I believe you need to add the following to the top of your script:

 using System.Text.RegularExpressions;

... or something like that.

That's why I said if you want to use a regex.

If you don't want to include that library (but get the same result) you can use the second example I gave.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Set name for game 0 Answers

TextField, Event.current, Input.GetKey, and GUI.FocusControl locking 1 Answer

Convert.ToInt32 error 1 Answer

Can you have a scrollview inside a textfield? 0 Answers

How to make a browse button to help the player load his own info file? 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