- Home /
character restriction
var text : string ;
function OnGUI () { GUI.skin.settings.cursorColor = Color.red; var chr : char = Event.current.character; if ( (chr < 'a' || chr > 'z') && (chr < 'A' || chr > 'Z') && (chr < '0' || chr > '9') ) { Event.current.character = '\0'; } text = GUI.TextField(new Rect(100,100,100,30), text,10); }
Assets/NewBehaviourScript.js(10,17): BCE0044: unexpected char: '''. error is coming can u rectify it
Answer by Statement · Mar 09, 2011 at 01:12 PM
I don't get that error mate, but I do get:
BCE0018: The name 'string' does not denote a valid type ('not found').
var text : string;
// Should be String, not string.
Then I get further errors:
-
BCE0051: Operator '<' cannot be used with a left hand side of type 'char' and a right hand side of type 'String'.
-
BCE0051: Operator '>' cannot be used with a left hand side of type 'char' and a right hand side of type 'String'.
I replaced the if-test to:
if ( !char.IsLetterOrDigit(chr) )
BCE0022: Cannot convert 'String' to 'char'.
Event.current.character = '\0';
This one was a bit more tricky. It seems (I am not a JS coder really) that you can't set characters with '. I set it to 0 instead. I tried out the modified code and it seems to work. (Another way to set the character would been Event.current.character = "\0"[0];
)
Here's the whole deal:
var text : String;
function OnGUI () { GUI.skin.settings.cursorColor = Color.red; var chr : char = Event.current.character; if ( !char.IsLetterOrDigit(chr) ) { Event.current.character = 0; } text = GUI.TextField(new Rect(100,100,100,30), text,10); }
can u please tell how above code work i am new to unity i tried hard to understand not getting any idea robert 0 secs ago
char.IsLetterOrDigit is a method that return true if the character is a letter, or if it is a digit. It might also support other characters such as **, so its logic is a bit different from the one you tried to write. So, we're using the ! operator which invert the result from char.IsLetterOrDigit, so the if test reads "if not a letter or digit". Event.current.character = 0 sets the character to value 0 (I am pretty sure it is same as \0). So, in effect, we're filtering out input when the typed character is anything else but a letter or digit.
Your answer
Follow this Question
Related Questions
Declaring char or string type variables returns errors 2 Answers
EnumPopup in Javascript? 2 Answers
sript gets error 2 Answers
HELP!! expecting :, found ';' problem 2 Answers
Singleton Error with code 1 Answer