Shiva
Shiva

Reputation: 1399

Why the javascript function shown below in the code section is not working in Firefox and Chrome? but works fine in IE

This code is responsible for preventing users from entering non-numeric characters such any any ascii except number [0-9]. Works fine in in IE, but not in Firefox and Chrome. Any help and suggestions are highly appreciated.

Thank you

'oKeyPress': function (e) {
    e = e || window.event;
    var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    return !(charCode > 31 && (charCode < 48 || charCode > 57));
}

Upvotes: 2

Views: 134

Answers (3)

Shiva
Shiva

Reputation: 1399

Thank you guys for your suggestions and feedback; I just found out why it did not work on Firefox and chrome before. The reason it was not working was because I was using something like this code from my Code-behind C# code :

this.txtApsId.Attributes.Add("onkeypress", "return (function(e) {var charCode = (navigator.appName == 'Netscape') ? e.which : e.keyCode; return charCode > 31 && (charCode < 48 || charCode > 57); }(event || window.event))");

Thanks

Upvotes: 0

svidgen
svidgen

Reputation: 14282

Use feature detection; not browser detection:

var charCode = e.charCode || e.keyCode;

Upvotes: 2

ebram khalil
ebram khalil

Reputation: 8321

inside your KeyPress event to get the charcode use this:

return (window.event ? e.keyCode : e.which)

Upvotes: 1

Related Questions