Validating a String

well this is a handy code snippet to validate a string
if you want to avoid some bad words, this is for you:

1
2
3
4
5
6
7
8
9
10
11
12
 
function isValid(str:String):Boolean {
        var regExp:RegExp = /\b(badword|nastyWord)\b/i;
	var _isValid:Boolean = !regExp.test(str);
        return _isValid;
}
 
var _inValidMessage = "this is a example for a bad badword it's really nasty";
var _validMessage = "Hello Word!";
 
trace(isValid(_inValidMessage)) //false
trace(isValid(_validMessage))//true

you can add more words just separating them by a pipeline ( | ) in the Regular Expression

Cheers.

This entry was posted in AS3, General and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*