2013-05-05 166 views
0

我从事输入验证(仅限kaybord的数字输入)。哪些工作正常,但问题是,我不能使用退格按钮。删除按钮效果很好。JavaScript验证 - 仅限数字输入和退格

我的代码:

function validate(evt) { 
    var theEvent = evt || window.event; 
    var key = theEvent.keyCode || theEvent.which; 
    key = String.fromCharCode(key); 
    var regex = /[0-9]|\./; 
    if(!regex.test(key)) { 
    theEvent.returnValue = false; 
    if(theEvent.preventDefault) theEvent.preventDefault(); 
    } 
} 

我知道我应该调整正则表达式,但即时通讯不太确定如何...... 需要帮助就这一个

非常感谢提前..

+0

var regex =/^ [0-9_ \ b] + $ /; - 仅限数字+退格 – Nita 2013-05-06 07:07:50

回答

0

它的全部工作..数字输入+工作bacspace

function validate(evt) { 
    var theEvent = evt || window.event; 
    var key = theEvent.keyCode || theEvent.which; 
    key = String.fromCharCode(key); 
    **var regex = /^[0-9_\b]+$/;** 
    if(!regex.test(key)) { 
    theEvent.returnValue = false; 
    if(theEvent.preventDefault) theEvent.preventDefault(); 
    } 
}