I'm writing a JavaScript application that will check user input for Carriage Returns (Hex 0D) and Line Feeds (Hex 0A), however, if the text is pasted into a web-form textarea field, the carriage returns disappear, leaving only the line feeds as line-breaking characters. I need to identify the carriage returns 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>
If I paste some text containing carriage-returns into the field, I'm never able to get a positive test for \r
, no matter what I paste.
Is this the normal behavior of a textarea field in a browser? Is there any way to preserve the carriage return characters along with the line-feeds in web-form input?