js 正则验证输入框只允许输入正整数或者负整数?

JavaScript024

js 正则验证输入框只允许输入正整数或者负整数?,第1张

<input type="number" name="" onkeydown="suibian(event)" />       function suibian(e){

        console.log(e)

        if((e.keyCode>=97&&e.keyCode<=105) ||(e.keyCode>=49&&e.keyCode<=57)){

          e.returnValue = true

          return

        }

        e.returnValue = false

      }

【参考资料】1.文本框只能输入数字代码(小数点也不能输入)<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">2.只能输入数字,能输小数点.<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"><input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能输入数字')this.value=''}">3.数字和小数点方法二<input type=text t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/)){this.value=this.t_value } else {this.t_value=this.value }if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)) {this.o_value=this.value"onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/)) {this.value=this.t_value }else {this.t_value=this.value }if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)) {this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/)){this.value=this.o_value }else if(this.value.match(/^\.\d+$/)) {this.value=0+this.value }if(this.value.match(/^\.$/)) {this.value=0 } this.o_value=this.value}">

<input type="text" value="" id="number">

<button class="btn">提交</button>

<script type="text/javascript">

$(function(){

    $(".btn").click(function(){ 

        var number = $('#number').val(),

            n = /^[1-9]\d*$/ 

        if(!n.test(number)){

            alert('请输入正整数')

        }

        

    })

})

</script>