473,836 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.returnVal ue=false;
event.cancelBub ble = true;
}
}
}

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

<body onkeypress="fnT rapKP();">

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
5 4593
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.returnVal ue=false;
event.cancelBub ble = true;
}
}
}

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

<body onkeypress="fnT rapKP();">

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="ret urn fnTrapKP();">
<textarea>Ent er not allowed</textarea>
<textarea onfocus="isBlur red=false;" onblur="isBlurr ed=true;">Enter
allowed</textarea>

--
Randy
comp.lang.javas cript 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.getEle mentsByTagName( "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.getEle mentsByTagName( "INPUT");
var textareas = document.getEle mentsByTagName( "TEXTAREA") ;
var i = inputs.length;
while(i-->0)
{
// note the triple ===; not a typo.
if(ignoredField s[inputs[i].name] || inputs[i].type!="text")
continue;
addKeyEvent(inp uts[i]);
}
i = textareas.lengt h;
while(i-->0)
{
if(ignoredField s[textareas[i].name)
continue;
addKeyEvent(tex tareas[i]);
}
}

function addKeyEvent(ele ment)
{
element.onkeypr ess = fnTrapKP;
}

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

</code>

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

<script>
ignoreField("my TextboxName");
</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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 13 '06 #5
Hi ian,

First, you will need to determine whether or not the textarea has the focus.
To do this, you need to create a variable to indicate whether or not it has
the focus, add an event handler for the "onfocus" event, and add an event
handler for the "onblur" event. Example:

<body onkeypress="Key Check(event)">

<script type="text/javascript"><!--
var textHasFocus = false;

function KeyCheck(e)
{
var KeyID = (window.event) ? event.keyCode : e.keyCode;
if (!textHasFocus && KeyID == 13)
{
event.returnVal ue=false;
event.cancelBub ble = true;
}
}
// --></script>
<form>
<textarea rows="2" name="S1" cols="20" onfocus="textHa sFocus=true"
onblur="textHas Focus=false"></textarea>
<input type="submit" value="Submit" name="B1">
</form>
</body>

Note that I also changed your function a bit, so that it will work in all
browsers (at least Mozilla and IE). In IE, there is a window.event object,
but not in Mozilla. In Mozilla, the event is passed to the event handler
from the object that raised it. So, the function handles the event according
to the browser it is run on.

What's going on here, is that the onfocus event happens when the cursor is
placed inside the textarea. The event handler sets the "textHasFoc us"
variable to true. When the user tabs out of the textarea, or clicks
somewhere else, or types outside the textarea, the "onblur" event sets the
"textHasFoc us" variable to false. The "KeyCheck" function checks the value
of this variable, and only disallows the ENTER key when it is false.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"ian" <is*******@hotm ail.com> wrote in message
news:11******** ************@g4 7g2000cwa.googl egroups.com...
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.returnVal ue=false;
event.cancelBub ble = true;
}
}
}

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

<body onkeypress="fnT rapKP();">

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 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
7620
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, I go well past the default max text length of the textbox. According to the documentation, setting MaxLength = 0 will increase this to basically the limit of available memory. So, in the form designer, I set the MaxLength property equal to 0. ...
9
1900
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 nothing happens. If I click on the Submit button its Click event fires as I'd expect. If I click anywhere else on the web page and then hit Enter, the button's Click event fires too. I want the Click event to fire when the user is in the Edit...
1
1232
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 code behind executed. so the question is: what should I do in order to obtain the desired behavior (provide a multiline input) ?
10
3067
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 number of rows. - Second column 25 %, no rowspan - Third column 25 %, no rowspan
16
8411
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 perfectly. Some do only allow numeric - but you cannot backspace. Maybe that is "good enough" - but surely it is not that difficult to have a textbox that only accepts numeric values and backspaces.
4
6821
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) {
4
3386
by: Benson Wong | last post by:
I add a TextBox with TextMode="MultiLine" so that I can input multilines to the textbox. That's fine. When user input <ENTERas a new line, it is ok during input to the textbox, but not ok when the textbox content is saved to database (<ENTERcharacter seems to be ignored, no new line appears when the data is saved to database). Any hints. Benson
3
5524
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 (as well as other controls). The textbox is set for multiline, so it *should* accept an Enter Key to move down to the next line. However, I'm finding that if I put the control onto a form which has a default Accept button set, then pressing...
0
1875
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
2255
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 that missing ability for other predefined controls in C#. The radiobutton, checkbox, and combobox controls do not share this ability. I find it difficult to believe that this feature was left out and Microsoft expects everyone to write...
0
9810
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9656
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10819
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10526
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10237
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9349
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7771
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5641
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3100
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.