473,977 Members | 51,426 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
4 6827
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

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

Similar topics

14
7634
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
1905
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
1236
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
3076
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
8441
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.
5
4602
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
5534
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
1886
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
2274
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
10356
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
10172
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
11409
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
10914
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
10085
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
8464
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
7610
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5159
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3764
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.