Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have merged html inputs in json format and after entering data into inputs or textarea   or
is added to the content.

What I have tried:

I have tried trim function but I could't solve is
Posted
Updated 15-Jun-21 3:11am

1 solution

It depends on where you're planning on removing the characters really. If you're using Javascript to build the JSON object then you can sanitise inputs or textareas by using regexp to match against potential whitespaces:
JavaScript
const value = textarea.value;
const regexp = /(<\s*br\s*\/?>|\s|&nbsp;)/ig;
const sanitised = value.replaceAll(regexp, '');

This won't account for other HTML elements however. If you want to make sure that HTML elements and HTML entities don't get rendered then you could consider sanitising the elements/entities as such:
JavaScript
const value = textarea.value;
const sanitised = value
  .replaceAll('&', '&amp;')
  .replaceAll('<', '&lt;')
  .replaceAll('>', '&gt;');
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900