0

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);"
3
  • 1
    "it works fine but it is not even accepting numeric." so, it doesn't work fine? Commented Feb 21, 2020 at 10:15
  • it is working fine means that i want to restrict user entering alphabets and special characters that is working but the issue is it is also restricting numeric input. Commented Feb 21, 2020 at 10:19
  • 2
    Working fine implies everything works. If not everything works, then that's not fine. If somebody is ill and bedridden, you don't describe them as "He is fine. He only can't leave bed and feels awful all the time." Commented Feb 21, 2020 at 10:24

2 Answers 2

2

Why not simply set the input type to number?

<input type="number" id="quantity" name="quantity" min="1" max="5">
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.