By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Only allow enter key in MultiLine textbox

P: n/a
ian
Hi,

I am currently using a Javascript function to dissallow the enter key
on my ASP.NET (2.0) web page, as follows:

function fnTrapKP(){
if (document.all)
{
if (event.keyCode == 13)
{
event.returnValue=false;
event.cancelBubble = true;
}
}
}

I call this from the body of my master page as follows:

<body onkeypress="fnTrapKP();">

On one of my pages I have a multiline textbox. The function above now
stops a user from untering multiple lines of text in to this textbox.

Is there any way I can modify the function to only allow the enter key
to be allowed when my multiline textbox has the focus?

Thanks

Ian

Jan 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ian said the following in news:comp.lang.javascript on 1/13/2006 at
11:35 AM:
Hi,

I am currently using a Javascript function to dissallow the enter key
on my ASP.NET (2.0) web page, as follows:
Let's be frank, up front. You are not disallowing it, you are attempting
to disallow it.
function fnTrapKP(){
if (document.all)
{
if (event.keyCode == 13)
If you use feature detection (the if(document.all) statement) then
feature detect for what you want to use.

if (event && event.keyCode && event.keyCode == 13)

{
event.returnValue=false;
event.cancelBubble = true;
}
}
}

I call this from the body of my master page as follows:

<body onkeypress="fnTrapKP();">

On one of my pages I have a multiline textbox. The function above now
stops a user from untering multiple lines of text in to this textbox.

Is there any way I can modify the function to only allow the enter key
to be allowed when my multiline textbox has the focus?


var isBlurred = true;
function fnTrapKP(){
if (event && event.keyCode && (event.keyCode==13) && isBlurred)
{return false;}
return true;
}

<body onkeypress="return fnTrapKP();">
<textarea>Enter not allowed</textarea>
<textarea onfocus="isBlurred=false;" onblur="isBlurred=true;">Enter
allowed</textarea>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 13 '06 #2

P: n/a
Why not use an init function instead?

do something like:

function initKeyEvent()
{
var inputs = document.getElementsByTagName("input");
var i = inputs.length;
while(i-->0)
{
if(inputs[i].type == "TEXT")
inputs[i].onkeypress = fnTrapKP;
}
}

that'll only pick up <input type="text" /> tags and not <textarea>
tags.

If you only want that textarea to be ignored, but pick up all the
others then try something like
<code>
// holds names or Ids or references to fields which are NOT to have the
event handler assigned.
// choose whichever identifier works best for you. I've stuck with name
'cos it's simple like me.
var ignoredFields = [];

function initKeyEvent()
{
var inputs = document.getElementsByTagName("INPUT");
var textareas = document.getElementsByTagName("TEXTAREA");
var i = inputs.length;
while(i-->0)
{
// note the triple ===; not a typo.
if(ignoredFields[inputs[i].name] || inputs[i].type!="text")
continue;
addKeyEvent(inputs[i]);
}
i = textareas.length;
while(i-->0)
{
if(ignoredFields[textareas[i].name)
continue;
addKeyEvent(textareas[i]);
}
}

function addKeyEvent(element)
{
element.onkeypress = fnTrapKP;
}

function ignoreField(fieldName)
{
ignoredFields[fieldName] = true;
}

</code>

now, at the bottom of the page with the multiline textbox use

<script>
ignoreField("myTextboxName");
</script>
That's all typed off the top of my head, so I apologise in advance for
fatal flaws, obvious typos, exploding monitors etc.

Jan 13 '06 #3

P: n/a
and the immediate fatal flaw is the "triple ===" comment where I was
using if(undefined===ignoredFields[someKey])

Jan 13 '06 #4

P: n/a
Flinky Wisty Pomm said the following on 1/13/2006 12:18 PM:
Why not use an init function instead?


<snip>

That looks like a whole lot of work to do what I did in a few lines :-)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.