views:

73

answers:

3

Hi Guys, I have a tiny function I use to only allow numeric input. It works great in IE, but I cannot get it to work in FireFox or Chrome. I have this js file loaded in my script tag of my HTML page.

var numberOnly = function(evt) { 

  var theEvent = evt || window.event; 
  var key = theEvent.keyCode || theEvent.which; 
  key = String.fromCharCode( key ); 
  var regex = /[0-9]|\./; 
  if( !regex.test(key) ) { 
    theEvent.returnValue = false; 

 } 
}; 

var wireElemToEvent = function(elemId, event, func){

var elem = document.getElementById(elemId);
if (typeof window.event !== 'undefined') {
    elem.attachEvent("on" + event, func);
} else {
    elem.addEventListener(event, func, true);
}
};



var wireEvents = function(){

wireElemToEvent("tbxQuantity", "keypress", numberOnly);
wireElemToEvent("tbxPhone", "keypress", numberOnly);
wireElemToEvent("tbxZip", "keypress", numberOnly);


};

window.onload = wireEvents;

Chrome tells me

file:///C:/Documents%20and%20Settings/xxx/Desktop/numbersonly/res/js/numbersonly.js:17Uncaught TypeError: Object # has no method 'attachEvent'

Any idea what I am doing wrong?

Thanks for any help, ~ck

A: 

One good way to make cross-browser scripting easier is to use jQuery. Plus, there's no reason to reinvent the wheel. Why not try this jQuery plugin: jQuery » numeric

Andy West
+1. Cross browser support will always lead back to jQuery :) Sooo much easier.
KP
I agree 100% and I use and love jQuery, however on this particular project, jQuery is not being used. The customer doesn't want any 3rd party libs.
Hcabnettek
Sorry to hear that. Does your customer know that jQuery is supported by Microsoft and will ship as part of Visual Studio 2010? http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx
Andy West
Lol they have no idea what Visual Studio is to begin with...
Hcabnettek
-1: The answer is not always JQuery. It is extremely easy to attach events in a cross-browser supported way without all the overhead.
Joel Potter
@Joel: You are right, of course. But using jQuery was merely a suggested alternative. I see no problem promoting the use of, and raising awareness of, a library that I like.
Andy West
@Andy: I like the library too, but is not a good answer to the current question (or at least there are better answers). I'd have upvoted you if you'd given a pure javascript solution AND suggested JQuery.
Joel Potter
@Joel: I often find the overhead (and there is very little) worth the time saved writing and debugging code like this.
Andy West
+1  A: 

Here is the function I use to attach events cross browser:

 function eventListen( t, fn, o ) {
  o = o || window;
  var e = t+fn;
  if ( o.attachEvent ) {
   o['e'+e] = fn;
   o[e] = function(){
    o['e'+e]( window.event );
   };
   o.attachEvent( 'on'+t, o[e] );
  }else{
   o.addEventListener( t, fn, false );
  }
 }

And to use it:

eventListen('message', function(e){
 var msg = JSON.parse(e.data);
    ...
});
Mic
+2  A: 

In wireElemToEvent You may want to check that elem is not null after you initialize it. Also, it would be better to check the existence of elem.attachEvent and elem.addEventListener rather than whether window.event is defined.

ntownsend