Instead of using this code (alert(err_str) ;) in all loop, move this at the end of the function.