• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1624
  • Last Modified:

Detecting CR/LF in text field input

I want to eliminate any user CR/LF (Enter character) keyboard input from text fields on an HTML form.

For example, on an address field, web visitors sometimes use the Enter key at the end of the field. This causes subsequent problems.

I'm thinking of just doing editing in Javascript in an onSubmit function, but I'm not sure how to detect the CR / LF characters in a browser and client platform independent manner.
Richard Korts
Richard Korts
1 Solution
Single line textfields cannot have CR/LF to them as far as I know and have always seen. However you can use:

myfield.value = myfield.value.replace(/[\r\n]/g,'');

Cem TürkSenior Software EngineerCommented:
<script type="text/javascript">
function processKey(ev, textbox) {
     switch(ev.keyCode) {
          case 13: { alert("you pressed enter");return false; }
     return true;
<textarea onKeyPress = "return processKey(event, this)"></textarea>

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now