Saving carriage returns (Hex 0D) in text areas

I am writing a JavaScript application that will check user input for carriage return (Hex 0D) and line feeds (Hex 0A), however, if the text is inserted into the web form text box, the carriage returns, leaving only the line feeds as line break characters. I need to identify carriage returns in order to properly process the data.

Here is a minimal example:

function checkForCR() {
  if (/\r/.test(document.crform.sandbox.value)) {
    alert('Carriage Returns found.');
  } else {
    alert('Carriage Returns NOT found.');
  }
  return false;
}
<form name='crform' onsubmit="return checkForCR();">
  <textarea name="sandbox" cols="50" rows="5"></textarea>
  <br />
  <input type="submit" value="Check for Carriage Returns">
</form>
Run codeHide result

If I insert any text containing a carriage return into the field, I can never get a positive test for \r, no matter what I insert.

Is this the usual behavior of a textarea field in a browser? Is there a way to save carriage return characters along with lines in web form input?

+4
1

:

/[\n\r]/g

if (/[\n\r]/g.test(document.crform.sandbox.value))

\n ( ) \r ( ).

0

All Articles