Link to home
Start Free TrialLog in
Avatar of Michael Krumpe
Michael KrumpeFlag for United States of America

asked on

Block Carriage-Return

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
Avatar of Linky
Linky

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.
ASKER CERTIFIED SOLUTION
Avatar of DireOrbAnt
DireOrbAnt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zvonko
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>