[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Block Carriage-Return

Posted on 2006-07-21
3
Medium Priority
?
444 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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

834 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