I am looking for a whitelist for a textarea that would be used for titles of books. The only characters I want to allow are alphanumeric, spaces, hyphens, underscores, periods, and the <br> tag. Any other special characters should be converted to their htmlentities ideally. The page uses php, html, javascript, and jquery if that helps. Anyone have any ideas??
Example input in textarea:
<textarea>
I have this book called Sample- a Fake Book.
</textarea>
If this has any relation to security, i.e. making sure the data is always safe for display, then this must be done server-side.
Except for the
<br>tag, just HTML-encoding should do what you want.Probably the best way would be to use htmlentities and then bring the
<br>‘s back:Another way to try to get this behavior and still use
htmlentitiesis to replace the<br>tags with a placeholder, run throughhtmlentities, then replace it back. Something along the lines of: