Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to replace window.event.Keycode = 0 for firefox

Posted on 2009-05-12
11
Medium Priority
?
2,512 Views
Last Modified: 2012-05-06
Hi
Working on  for Cross browser support, facing an issue, with window.event.Keycode = 0 is not working in firefox as well safari.
For onKeyPress it should not allow any charecters in the text box.
0
Comment
Question by:sandeep1983
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 15

Expert Comment

by:fsze88
ID: 24365567
LIIKE THIS?
<script language="javascript">
 
function getKeyevent(e){
var keynum;
var ctrlKey;
var eventObj;
ctrlKey = false;
 
if(window.event) // IE
	{
            	keynum = event.keyCode;
            	ctrlKey  = event.ctrlKey;
            	EventObj = event.srcElement;
            	charCode = String.fromCharCode(keynum); 
 	}
else if(e.which) // Netscape/Firefox/Opera
	{
            	keynum = e.which;
            	ctrlKey = e.ctrlKey;
            	EventObj = e.target;
            	charCode = String.fromCharCode(keynum);
	}
  alert(keynum);
 
}
</script>
<body onkeydown="return getKeyevent(event);" >
 
</body>

Open in new window

0
 

Author Comment

by:sandeep1983
ID: 24366275
if( regularEpressioDigit.test(keyPressed) == false )
    {
            window.event.keyCode = 0;
          
        
    }
Can I substitute this with anything instead of Browser forking

0
 
LVL 43

Expert Comment

by:David S.
ID: 24369366
Here's another way to write the function fsze88 posted.

If you simply want to prevent someone from typing a character, it would be better to set the field as readonly.  Alternatively you could cancel the event instead of changing the keyCode.
function getKeyevent(e){
  e = e||window.event;
  var keynum = e.which||e.charCode||e.keyCode;
  var charCode = String.fromCharCode(keynum); 
  var ctrlKey = false;
 
  var EventObj=e.target||e.srcElement;
 
  alert(keynum);
 
}

Open in new window

0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

Author Comment

by:sandeep1983
ID: 24431509
Hi

Can anyone please help, how to nullify an event key press when charecters are enterend and oly numerics are accepted
similar to window.event.keycode =0
which would support all browsers, especially safari and firefox

event.reteturnValue = false (this approch was not useful
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24454018
The html page below is cross-browser and how I have prevented non-numeric keys from being used in a field.  Let me know if you need help using it in your page.  Note the return that is used in the event.  The key is really just the function and the value for the event in the input box.
bol

<!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" xml:lang="en" lang="en">
<head>
<link href="/style.css" rel="stylesheet" type="text/css" />
<title>Validation</title>
<script type="text/javascript">
function isNumeric(e) {
	var k = (e.which)? e.which : e.keyCode;
//	document.getElementById('status').innerHTML = (k > 31 && (k < 48 || k > 57) && (k != 46))? 'not number ' + k : 'number ' + k;
	return (k < 48 || k > 57)? false : true;
}
</script>
</head>
 
<body>
<h3>Is Numeric</h3>
 
<br /><br />
Type: <input type="text" name="input1" onkeypress="return isNumeric(event);" />
&nbsp;&nbsp;&nbsp;
<span id="status"></span>
 
</body>
</html>

Open in new window

0
 

Author Comment

by:sandeep1983
ID: 24490950
Can you modify the above code without using Document.Selection to validate Decimal upto 2 and numerics upto 7 total 9 digits

Intention to support all browsers
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 2000 total points
ID: 24494937
Sure.  Use the code below.  Make sure you test it for various cases.  The function needs to go in the head part of the html with the other function and the input is modified as you see in the code.
Let me know if you have a question.
bol

<input type="text" name="input1" onkeypress="return isNumeric(event);" onblur="return checkValue(this);" />
 
function checkValue(num) {
    if (!num.value.match(/^(\d{1,9}|\d{1,8}\.\d{1}|\d{1,7}\.\d{2})$/)) {
        alert("Please enter a number with only up to 9 digits and using no more than 2 decimal places.");
        num.value = "";
        return false;
    }
    return true;
}

Open in new window

0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24815243
Why the B grade?  It seems like I provide details and the exact answer you needed.  I even added an extra validation that, frankly, wasn't part of the original question.  Let me know if the grade was a mistake because that can be easily corrected.  If there is a reason for it then please respond to explain.  If you aren't real sure what the grading means on EE then let me know.
bol
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 25154068
Grade changed to 'A'
 
modus_operandi
EE Moderator
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question