如何修改此功能,以便它也允许数字

时间:2014-11-19 15:52:19

标签: jquery

我有这个功能,不允许任何特殊字符

请让我知道如何修改此功能,以便它也允许数字?

<input type="text" id="category"  onkeypress="return nospecialCharacters(event,this)"  class="m-wrap span12" placeholder="">


function nospecialCharacters(thi,dec)
{
    if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return true;

   if (((keycode>=65) && (keycode<=90))||((keycode>=97) && (keycode<=122)) || keycode==32)
   {
        return true;
   }
   else
   {
        return false;
   }
} 

这是我的jsfiddle

http://jsfiddle.net/z167ac64/

1 个答案:

答案 0 :(得分:1)

使用ASCII表代码:

enter image description here

<input type="text" id="category"  onkeypress="return nospecialCharacters(event,this)"  class="m-wrap span12" placeholder="">


function nospecialCharacters(thi,dec)
{
    if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return true;

   if (((keycode>=48) && (keycode<=57))||((keycode>=65) && (keycode<=90)) ||((keycode>=97) && (keycode<=122)) || keycode==32)
   {
        return true;
   }
   else
   {
        return false;
   }
}