简体   繁体   中英

Javascript function not working with Internet Explorer

this is my javascript function to prevent user not to enter alphabets and special characters in the textbox field, it works fine but it is not even accepting numeric.

please note that it is working fine Firefox.

function isNumber(event) {
            var regex = new RegExp("^[0-9]+$");
            var str = String.fromCharCode(!event.charCode ? event.which : event.charCode);
            if (regex.test(str)) {
                return true;
            }
            if (window.event) {
                window.event.returnValue = false;
            }

            return false;
        }

and this is my textbox

<asp:TextBox Width="50px" ID="placesvis" runat="server" onkeypress="isNumber(event);"

为什么不简单地将输入类型设置为数字?

<input type="number" id="quantity" name="quantity" min="1" max="5">

isNumber is not receives event object, it is receiving textbox as a parameter.

you could try this: document.getElementById("placesvis").addEventListener('keypress', isNumber);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM