Better method to detect illegal characters