String validation and sanitization in JavaScript