• 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.
0
Richard Korts
Asked:
Richard Korts
1 Solution
 
RoonaanCommented:
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,'');

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

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