473,396 Members | 2,020 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,396 software developers and data experts.

Detect a specific keypress

Hi,

I have a text box where the user enters their employee email address.
What I want to occur is when the @ sign is pressed it will
automatically add the rest (ie. mycompany.com)

I know the code required to alter the value of a text box but I dont
know the event trigger or code to detect the keypress.

Any ideas?

Cheers

Burnsy

Nov 7 '05 #1
8 6555
wrote on 07 nov 2005 in comp.lang.javascript:
I have a text box where the user enters their employee email address.
What I want to occur is when the @ sign is pressed it will
automatically add the rest (ie. mycompany.com)

I know the code required to alter the value of a text box but I dont
know the event trigger or code to detect the keypress.


don't use keypress, but keyup:

<input onkeyup='var x=this.value;
if(x.substr(x.length-1,1)=="@")
this.value=x+"mycomp.com"'>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 7 '05 #2
Thank you, works a treat.

Now, my next idea ...

Is it possible to highlight specific characters in a text box? For
example, if I have the value 'j*****@mycomp.com', could I highlight
from position 7 to the end (value.length) resulting in the string
'mycomp.com' being selected?

Cheers

Burnsy

Nov 8 '05 #3
You can in a textarea, through various browser-dependent methods. I
don't think you can in a normal type="Text" input though.

Here is some code I wrote as part of an editor that will handle setting
the selected text in a textarea.
function _mozSetSelection(txtArea, start, end){
txtArea.selectionStart = start;
txtArea.selectionEnd = end;
}

function _ieSetSelection(txtArea, start, end){
var txtRange = document.selection.createRange();
if (txtRange){
//Put the cursor at the very start of the text area.
if (txtRange.moveToElementText){
//The IE way.
txtRange.moveToElementText(txtArea);
txtRange.setEndPoint('EndToStart', txtRange);
}
else {
//The Opera way.
txtRange.moveStart('character', -txtArea.value.length);
txtRange.moveEnd('character', -txtArea.value.length);
}
txtRange.moveStart('character', start);
txtRange.moveEnd('character', end-start);
txtRange.select();
}
}

function setSelection(txtArea, start, end){
(navigator.userAgent.indexOf('Gecko') ==
-1)?_ieSetSelection(txtArea, start, end):_mozSetSelection(txtArea,
start, end);
}

You would use it like this:
setSelection(textareaBox, 7, textareaBox.value.length);

Nov 8 '05 #4
Ok, havent yet tried it in other browsers but having trouble with IE.

I have simplified it down to the following:

function _ieSetSelection(oTextbox, start, end){

var oRange = document.selection.createRange();

if (oRange) {
//Put the cursor at the very start of the text area.
if (oRange.moveToElementText){
//The IE way.
oRange.moveToElementText(oTextbox);
oRange.setEndPoint('EndToStart', oRange);
}
txtRange.moveStart('character', start);
txtRange.moveEnd('character', end-start);
txtRange.select();
}

}

The HTML element I am using is:

<input name="email" type="text" value="1234567890"
onmouseover="_ieSetSelection (this, 3, 5);">

What I am trying to make happen initially is when the cursor rolls over
the textbox, using the provided code, it will select the text. Without
having a good enough understanding of the textRange object (MS site not
too informative on the subject), I dont know whats wrong.

Burnsy

Nov 9 '05 #5
Sorry, changed the names and forgot others. Still not working though:
function _ieSetSelection(oTextbox, start, end) {

var oRange = document.selection.createRange();

if (oRange) {
//Put the cursor at the very start of the text area.
if (oRange.moveToElementText){
//The IE way.
oRange.moveToElementText(oTextbox);
oRange.setEndPoint('EndToStart', oRange);
}
oRange.moveStart('character', start);
oRange.moveEnd('character', end-start);
oRange.select();
}
}

Nov 9 '05 #6

bi******@yahoo.co.uk wrote:
Sorry, changed the names and forgot others. Still not working though:
function _ieSetSelection(oTextbox, start, end) {

var oRange = document.selection.createRange();

if (oRange) {
//Put the cursor at the very start of the text area.
if (oRange.moveToElementText){
//The IE way.
oRange.moveToElementText(oTextbox);
oRange.setEndPoint('EndToStart', oRange);
}
oRange.moveStart('character', start);
oRange.moveEnd('character', end-start);
oRange.select();
}
}


Hi. It works fine for me when I test it. You may have an error or
wrong assumption somewhere else in your script.

Julian

Nov 9 '05 #7
> It works fine for me when I test it.

Is it designed to work for a <textarea> or an <input... ? I got it
working on a <textarea> but not an input.

Nov 9 '05 #8
> I don't think you can in a normal type="Text" input though. .

Sorry, my mistake. Not reading posts through enough

Nov 23 '05 #9

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

Similar topics

6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
3
by: Tegdeep | last post by:
Here's what I want to do: I have a hash table which contains data associated to different keys. The Hash keys are represented by a single character from the keyboard: 0-9, a-z, A-Z, and the...
3
by: birarai | last post by:
How do i get the 'keypress' event to capture only the 'enter' key event? The 'keypress' event captures all key press events. input.addEventListener('keypress', sendButtonPressed, true); ...
3
by: Boki | last post by:
Hi All, Could you please advice that how to detect the enter "key" event.... due to I don't want another button to do "Go" function..... Best regards, Boki.
0
by: Rich | last post by:
Hello, I have been using "Concole.WriteLine("Event Name") for almost every event in the datagridview control, but I can't get the KeyPress event to file. My goal is to force uppercase on the...
1
by: mark4asp | last post by:
<!-- // How to detect the rendering mode which the browser is currently in (works for IE6). // Ctrl+Shift+s displays indicates whether the browser is in quirks or standards mode. // Detect...
1
by: Alex Taslab | last post by:
Hi everybody, does anyone know how to detect a key press from a keyboard. Well I do know how to do that, but i need to detect just one press and ignore the others. I mean, my program checks an...
0
by: thiruscripts | last post by:
Hi, I need to detect the keyvalue while keypress. ie irrespective to control. For example if i press some characters in textbox the some event to be triggered. In the condition the event should be...
4
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.