[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

CrossBrowser Javascript

I have 2 Javascript functions, both of which work in IE but not in Firefox, I would appreciate it if someone could help me re-write both so that they are crossbrowser compatible.
function OnlyDigits()
{
 var evtObj=window.event;
 if (evtObj.keyCode==13) return;
    var filter = /^[0-9]$/;
    
    if (!filter.test(String.fromCharCode(evtObj.keyCode)))//do not allow these characters
    {
        evtObj.returnValue=false;                    
        return;
    } 
}
function toUpper()
{   var evtObj=window.event;
    var filter = new RegExp();
    var filter = /^&|;|'|"|`|<|>|,|\?|%|\$|\|$/; 
    if(evtObj.ctrlKey || evtObj.altKey)
        return;
    if (filter.test(String.fromCharCode(evtObj.keyCode)) == true)//do not allow these characters
    {
        evtObj.returnValue=false;                    
        return;
    }
    evtObj.keyCode=String.fromCharCode(evtObj.keyCode).toUpperCase().charCodeAt(0);
    
}

Open in new window

0
diarmuidflaherty
Asked:
diarmuidflaherty
2 Solutions
 
cstsangCommented:
Try this one
function OnlyDigits()
{
 var evtObj=(window.event? event : E);
 if (evtObj.keyCode==13) return;
    var filter = /^[0-9]$/;
    
    if (!filter.test(String.fromCharCode(evtObj.keyCode)))//do not allow these characters
    {
        evtObj.returnValue=false;                    
        return;
    } 
}
function toUpper()
{  var evtObj=(window.event? event : E);
    var filter = new RegExp();
    var filter = /^&|;|'|"|`|<|>|,|\?|%|\$|\|$/; 
    if(evtObj.ctrlKey || evtObj.altKey)
        return;
    if (filter.test(String.fromCharCode(evtObj.keyCode)) == true)//do not allow these characters
    {
        evtObj.returnValue=false;                    
        return;
    }
    evtObj.keyCode=String.fromCharCode(evtObj.keyCode).toUpperCase().charCodeAt(0);
    
}

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
you forgot to pass the E

function OnlyDigits(E)

function toUpper(E)

and you need to add it to the handler

onKey...=OnlyDigits(event)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now