如何在JavaScript中使用箭头键忽略特殊字符?

时间:2017-05-01 03:54:28

标签: javascript

我使用JavaScript忽略文本框中的特殊字符。 但我需要允许箭头键操作。

所以,我允许

  • left = 37
  • up = 38
  • right = 39
  • down = 40

我的文本框中的键。

我使用以下函数,

function checkKeys(event){
 // Getting the event to be triggered.
var theEvent = event || window.event;
// Getting the type of event or code.
var key = theEvent.keyCode || theEvent.which;

// Check with list of code and ignore holding.
// Tab, Space, Home, End, Up, Down, Left, Right...
if (key === 9 || key === 32 || key === 13 || key === 8 || (key >= 35 && key <= 40)) { //TAB was pressed
    return true;
}

// If not in list then check return with corresponding data.
key = String.fromCharCode(key);
// Return also if length is 0.
if (key.length == 0) return true;

// Finally return "false" for general keys.
return false;
}

但是我发现这个键的另一个问题。

  • left = 37 =%
  • up = 38 =&amp;
  • right = 39 ='
  • down = 40 =(

相同的导航键与特殊字符键相同。

这有什么不对?

如何限制在文本框中使用特殊字符[%,&amp;,',(]?

0 个答案:

没有答案
相关问题