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
9
Question by BrUnO-XaVIeR · Jun 03, 2010 at 03:47 PM · guitextguitext

Restrict characters in GUI.TextField

Hi. How could I ban some letters from being typed into a TextField? For example, for a UserName field, the player can't write charaters like "[]{}!@#$%&*()", etc. How could I prevent the player to type those characters into the TextField?

Thanks in advance.

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

4 Replies

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

Answer by qJake · Jun 03, 2010 at 05:18 PM

There are lots of ways to do this, but the best way is to use RegEx.

You'd want to use something like this:

using System.Text.RegularExpressions; // needed for Regex

public class TextBox : MonoBehaviour { string text;

  void OnGUI()
  {
       text = GUI.TextField(..., text);
       text = Regex.Replace(text, @"[^a-zA-Z0-9 ]", "");
  }

}

That code will only allow characters a-z, A-Z, 0-9, and a space (" "). If you want to include other characters, you can add them inside the brackets, like this:

[^a-zA-Z0-9 #!@]

But be sure to leave the ^ at the start of the brackets.

MSDN Reference for Regex:

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

Comment
Add comment · Show 10 · 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 BrUnO-XaVIeR · Jun 03, 2010 at 06:18 PM 0
Share

Thank you. This is really useful!

avatar image Molix · Jun 04, 2010 at 01:54 AM 0
Share

FYI: to get this to compile I had to add using System.Text.RegularExpressions, and add an empty string to the Replace call (i.e. replace other characters with nothing).

avatar image qJake · Jun 04, 2010 at 08:37 AM 0
Share

Ah, whoops. The joys of writing code without a compiler to check it. ;)

avatar image lbalasubbaiahapps · Oct 20, 2011 at 07:44 AM 0
Share

thanks SpikeX . its awesome . It's working.

avatar image Kehos · Jan 24, 2013 at 02:57 PM 0
Share

Thanks a lot for the solution!!

Show more comments
avatar image
24

Answer by Eric5h5 · Jun 03, 2010 at 07:08 PM

The problem with System.Text.RegularExpressions is that it adds about 900K to web player sizes, because of having to add .dlls that aren't normally included. Also doing regex string replacements every frame in OnGUI may not be ideal for performance. If you're not using a web player then you probably wouldn't care, but if you are, you would likely be better off preventing the unwanted characters from entering the string in the first place and not using regex:

void OnGUI () {
    char chr = Event.current.character;
    if ( (chr < 'a' || chr > 'z') && (chr < 'A' || chr > 'Z') && (chr < '0' || chr > '9') ) {
        Event.current.character = '\0';
    }
    text = GUILayout.TextField(text, GUILayout.Width(500));
}

That replaces any input character not in the a-z, A-Z, and 0-9 range with a null character.

Comment
Add comment · Show 7 · 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 BrUnO-XaVIeR · Jun 03, 2010 at 08:29 PM 0
Share

Thank you. I'll use this for Webplayer projects. Thanks.

avatar image Molix · Jun 04, 2010 at 01:51 AM 1
Share

That's definitely the way to go; both of the string replacement versions suffer from allowing the cursor to move even if the character is not added to the string, e.g. type "abcdef", then cursor back to the 'c' and try to insert an invalid character.

avatar image Eric5h5 · Jun 04, 2010 at 03:47 AM 0
Share

Yay, brute force program$$anonymous$$g wins over elegant program$$anonymous$$g! ;)

avatar image qJake · Jun 04, 2010 at 08:38 AM 0
Share

Aw, noone likes Regex... :(

avatar image qJake · Jun 05, 2010 at 08:08 AM 0
Share

You know what would be nice is if someone wrote a small wrapper for the syntax for Regex ([ ... ], and [^ ... ] for negation), and wrapped that into a small class to streamline what you've wrote here. It's good code, but it's extremely messy.

Show more comments
avatar image
3

Answer by Molix · Jun 03, 2010 at 05:18 PM

There may be a more efficient way, but an easy way is:

private string enteredString = ""; private char[] badChars = { '[', ']', '{', '}', '!', '@', '#', '$', '%', '', '&', '*', '(', ')', '', '', '', '', '', '', '' };

void OnGUI() { string newString = GUILayout.TextField( enteredString ); if( newString.IndexOfAny( badChars ) < 0 ) enteredString = newString; }

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 qJake · Jun 03, 2010 at 05:19 PM 1
Share

Yep, there's a better way... Regex! :)

avatar image BrUnO-XaVIeR · Jun 03, 2010 at 06:17 PM 0
Share

Thank you for your help.

avatar image Molix · Jun 04, 2010 at 01:55 AM 0
Share

I'd use Event solution above (I tried to down-vote my answer but couldn't :P

avatar image
2

Answer by mehtanitish · Dec 22, 2016 at 10:02 AM

Hi @Bruno,

You can use ContentType property on input field. Say, you want only alphabets to be typed in your input field as name for that you can use Name variable. You can put the following line of code in Start function for that:

InputFieldref.contentType = InputField.ContentType.Name;

Using above line enforces capitalisation and the InputField is used for typing in a name.

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

changing font + size of text 1 Answer

How to make text damage notifiers above enemy 1 Answer

Script to change GUI text's displayed text value? 7 Answers

HELP with scripting where GUI is involved 2 Answers

GUIText not working properly with a timer. 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