Solved

Block Carriage-Return

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now