Reply
Allow only caps at key press in firefox
Old 06-09-2006, 07:22 AM Allow only caps at key press in firefox
Junior Talker

Posts: 2
in IE i m getting ascii/unicode using event.keyCode..

for "a" i m getting 97. and when i assign as below..

i m getting it in variable i...

event.keyCode = i - 32. then it will be converted in "A" and will be displayed in textbox.

but it is not working with firefox.... if anybody have any idea then please tell me.. i need it urgently.......


i tried with followed event methods...

event.keyCode, event.charCode, event.which........
gautamtrivedi is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 06-09-2006, 01:32 PM Re: Allow only caps at key press in firefox
Christopher's Avatar
Iced Cap

Latest Blog Post:
PHP and Unicode with UTF-8
Posts: 3,108
Location: Toronto, Ontario
window.event is an IE-specific thing. W3C-compliant browsers (read: mostly everything but IE ;P) should pass event data to the function, just make your function accept a parameter ('e' for example). Here is an example that probably suits your needs exactly, right from QuirksMode:

Code:
function doSomething(e)
{
    if (!e) var e = window.event
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    
    // Use code as the keycode for all browsers
}
You can find this code snippet and more useful info at QuirksMode: http://www.quirksmode.org/js/introevents.html
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 06-10-2006, 02:04 AM Re: Allow only caps at key press in firefox
Junior Talker

Posts: 2
following is the code i m running... but as per your answer its not working...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function doSomething(e)
{
if (!e) var e = window.event

if(e.keyCode){

code = e.keyCode;
if(code > 64 && code < 91)
e.keyCode = code - 32;

}else if(e.which){

code = e.which;
if(code > 96 && code < 122)
e.which = code - 32;

}

}
</script>
</head>

<body>
<input type="text" id="txt" name="txt" onkeyup="doSomething(event)"/>
</body>
</html>
gautamtrivedi is offline
Reply With Quote
View Public Profile
 
Old 06-10-2006, 01:22 PM Re: Allow only caps at key press in firefox
Christopher's Avatar
Iced Cap

Latest Blog Post:
PHP and Unicode with UTF-8
Posts: 3,108
Location: Toronto, Ontario
To W3C browsers, it is only information. See for an example of how to do what you want cross browser.

But I think you are going about it in a more complicated way then is necessary. You can just toUpperCase it on onkeyup and you can get almost the same effect.


Code:
<input type="text" id="txt" name="txt" onkeyup="this.value=this.value.toUpperCase()" />
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 06-10-2006, 10:43 PM Re: Allow only caps at key press in firefox
Ultra Talker

Posts: 252
Location: Auckland, New Zealand
Chroder's method is probably the better way than trying to override an event, especially when those events should be read-only it's just IE seems to think they can be overriden.

Although you could use onkeyup or onblur, I would prefer changing it when focus is lost (onblur) than calling it on each keyup.

Shouldn't really matter, because your script that handles the form should convert it to uppercase anyways instead of relying on the input to be uppercase.

If you want to capture characters a more compatible way then:

Code:
var ch = (event.keyCode) ? event.keyCode : event.charCode;
event.which is a real old method which was used in Netscape and does work in Firefox, but I find charCode more compatible for all other browsers, keyCode is IE specific but a few browsers may support it too.

Events should be read-only, so you can't override them, just seems Firefox has kept it that way.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.

Last edited by mastercomputers : 06-10-2006 at 10:47 PM.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to Allow only caps at key press in firefox
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13240 seconds with 13 queries