input type类型为number时,可以输入'e',验证过滤

input type类型为number时,可以输入'e',验证过滤

<input type="number" onkeydown="return FilterInput(event)" onpaste="handlePaste(event)" >

<script>
function FilterInput(event) {
    var keyCode = ('which' in event) ? event.which : event.keyCode;

    isNotWanted = (keyCode == 69 || keyCode == 101);
    return !isNotWanted;
};
function handlePaste (e) {
    var clipboardData, pastedData;
    // Get pasted data via clipboard API
    clipboardData = e.clipboardData || window.clipboardData;
    pastedData = clipboardData.getData('Text').toUpperCase();
    if(pastedData.indexOf('E')>-1) {
        //alert('found an E');
        e.stopPropagation();
        e.preventDefault();
    }
};
</script>

参考地址

keycode

标签: javascript

添加新评论