?
Solved

Block Carriage-Return

Posted on 2006-07-21
3
Medium Priority
?
433 Views
Last Modified: 2008-01-09
I have an input box for freetext. I am already blocking the number of characters the user is allowed to type.
 onkeypress="javascript:if(this.value.length >= 90){return false};"

How do I check for when the Enter Key (Carriage Return) is pressed and block it?
So that someone cannot enter

texttexttext
texttext
text

Because it must be inline, like

texttexttexttexttexttext
0
Comment
Question by:Michael Krumpe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:Linky
ID: 17157287
Well you can just use <input type="text" name="data" value=""> and they cannot use carriage return at all. Or you can do:

for(i=0; i<str.length; i++){
    if(str.indexOf("\n") > -1){
        str= str.replace("\n", "");
    }
}

That code is from memory, so you might want to play around which it. The ascii character for carriage return is 13, so I think you can do replace(chr(13), '') and achieve the same effect.
0
 
LVL 26

Accepted Solution

by:
DireOrbAnt earned 2000 total points
ID: 17157294
This works in IE:

<TEXTAREA COLS="20" ROWS="20" NAME="Yo" ID="Yo" onkeypress="CheckField(event, this)"></TEXTAREA>
<SCRIPT TYPE="text/javascript">
function CheckField(e, Obj) {
  var CharKey = window.event ? e.keyCode : e.which;
  if (Obj.value.length >= 90 || CharKey == 13) {
    if (window.event) e.keyCode = null;
    return false;
  }
  return true;
}
</SCRIPT>

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17158149
Like this:

<input type="text" name="yourText" size="90" maxlength="90">

And if you have to use textarea, then check this:
<textarea name="yourArea" onKeyUp="if(this.value.match(/[\n\r]/))this.value=this.value.replace(/[\n\r]/g,'');if(this.value.length>90)this.value=this.value.substr(0,90);"></textarea>

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question