Understanding Event.keyCode Restrictions on Text Input

Asked 2 years ago, Updated 2 years ago, 97 views

Thank you for your help.

Windows, VisualStudio 2015 ASP.NET (VisualBasic), .NET Framework 4.6,
Local IIS, browsers are developed in Google Chrome, IE, and .

Put text (TextBox in ASP.NET, named txtTest) on one page and

Only backspace, tabs, front and rear arrows, and half-width numbers can be entered. Set the function (named numOnly())

It is related to what I asked you before.
text in HTMLIf you enter only half-width numbers in onkeydown of t, the numeric keypad does not work.

I have a grammar question.

Write the HTML from the web page source as follows:

function numOnly(){
            if((48<=event.keyCode)&(event.keyCode<=57){
                return true;
            } else{
                return false;
            }
        }

If you write like this, you can enter only half-width numbers.
No backspaces, tabs, front and rear arrows, or half-width alphanumeric characters.
Therefore, regarding the value of event.keyCode, use the backspace = 8, tab = 9, front and rear arrows = 37 and 39, and
Rewrite as follows:

function numOnly(){
        if(event.keyCode=8){
            return true;
        }
        else if(event.keyCode=9){
            return true;
        }
         else if (event.keyCode=37) {
            return true;
        }
         else if(event.keyCode=39){
            return true;
        }
         else if((48<=event.keyCode)&(event.keyCode<=57){
            return true;
        } else{
            return false;
        }
    }

However, if you write like this, you can enter half-width English characters.
I don't know why.

If there is a defect in the writing method,
I would appreciate it if you could let me know.

I look forward to your kind cooperation.

javascript html css visual-studio asp.net

2022-09-30 19:51

1 Answers

The conditional event.keyCode=8 is substituted (=), so please rewrite it to equal value comparison (==).


2022-09-30 19:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.