473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Only allow enter key in MultiLine textbox

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
4 6803
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
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
and the immediate fatal flaw is the "triple ===" comment where I was
using if(undefined===ignoredFields[someKey])

Jan 13 '06 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Adam Clauss | last post by:
I've an application which is using a multiline textbox to log the status of a fairly long procedure. "Updates" are made to the status by calling textbox.AppendText. As my task is fairly lengthy,...
9
by: Nate Hekman | last post by:
As I've mentioned a couple of times in the last few minutes(!), I've got a simple form with an edit box and a Submit button. If I type something in the Edit box and hit Enter I hear a click but...
1
by: Antonio Lopez Arredondo | last post by:
hi all !!!!! I have an ASP.NET app that has a multiline textbox and a BUTTON. the problem I have is that whenever I press ENTER in the multiline textbox, the button event is raised and the...
10
by: Perry van Kuppeveld | last post by:
Hi, I have a problem with formatting a table including text fields wich can contain up to 255 chars. I need a table with 3 columns: - First column 50 % over the with a rowspan of the total...
16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
5
by: ian | last post by:
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) {
3
by: Simon Verona | last post by:
Sorry for the repost, but this group seems to be more "active" than the group that I posted my question, and it's probably as valid here as there! I have a usercontrol, which contains a textbox...
0
by: Steve K | last post by:
to this newline when an enter key is detected. Update: I tried adding this code: <code> public PMDDataGridViewTextBoxEditingControl() :base() { this.Multiline = true; } </code>
23
by: Dan Tallent | last post by:
A textbox has a attribute for ReadOnly. This seems like such a simple concept. When a textbox is set to read only the user cannot change the contents of the field. I have been trying to find...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.