• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1759
  • 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>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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