Solved

Block Carriage-Return

Posted on 2006-07-21
3
426 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
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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

776 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