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

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);
}