Sanitizing Input in Flash

article_flash

Let’s say for example you are creating a bulletin board system or some post system from within flash and you would like to restrict the use of any html or special characters. It turns out there is a very simple and elegant way you can strip out or replace any characters you want to.

First we want to declare an array in as3 with all the characters that we don’t want in our string.

var htmlChars:Array = new Array();

htmlChars["\\"] = "";
htmlChars["\""] = "";
htmlChars[">"] = "";
htmlChars["<"] = "";

Then all we have to do is to create our function that takes a string as an argument and returns our safe sanitized string.

function sanitizeInput(msg:String):String
{
	for (var htmlChar:Object in htmlChars)
	{
    	     msg = msg.split(htmlChar).join(htmlChars[htmlChar]);
	}
	return msg;
}

Now we can call our function.

var safeString:String = sanitizeInput("

What we can also do is to replace characters in the string. For example…

htmlChars["Bye"] = "CYA";

var safeString:String = sanitizeInput("Bye for now\");
// safeString = "CYA for now"

Leave a Comment

Back to Top