php验证数字100倍数,js如何实现一个文本框只能输入数字 且是100的倍数
js如何實現(xiàn)一個文本框只能輸入數(shù)字 且是100的倍數(shù)?
php
var a = 123,b = 200;
/\d/.test(a) && a % 100 == 0;//false
/\d/.test(b) && b % 100 == 0;//true
用keydown過濾掉數(shù)字以外的,然后再乘100
<input ?id="num" οnkeydοwn="onlynum();"/>
<script>
function onlynum(){
if ( ! ((event.keyCode >= 48 && event.keyCode <= 57 ) || (event.keyCode >= 96 && event.keyCode <= 105 ) || (event.keyCode == 8 )))
event.returnValue = false ;
}
</script>
沒寫100的倍數(shù),但是應該差不多是這個樣子。
但是為了使其能更具有普遍的兼容性,最好用event.keyCode|| event.which.
===差不多就是這個樣子,自己試一下吧====
<!DOCTYPE html>
<html>
<head>
<title>Number Entry</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function(){
var keyupTimeout;
$('#numberEntry').on("keyup",function(ev){
var keycode = ev.which;//keycode就是按鍵的編碼
var currentValue=$('#numberEntry').val();
currentValue=parseInt(currentValue,10)||0;
currentValue=currentValue/100;
clearTimeout(keyupTimeout);
if((!(keycode>=48&&keycode<=57)||(keycode>=96&&keycode<= 105))&&(keycode!==8)){
$('#numberEntry').val($('#numberEntry').val().substring(0,$('#numberEntry').val().length-1));
return;
}
keyupTimeout=setTimeout(function(){
if((keycode>=48&&keycode<=57)||(keycode>=96&&keycode<= 105)||keycode==8){
if(Math.floor(currentValue) !== currentValue){
$('#numberEntry').val('');
}
}
},400);
});
});
</script>
</head>
<body>
輸入數(shù)字且是100的整數(shù)倍:
<input type="text" value="" id="numberEntry">
</body>
</html>
總結
以上是生活随笔為你收集整理的php验证数字100倍数,js如何实现一个文本框只能输入数字 且是100的倍数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: appium java 测试用例_如何在
- 下一篇: 动态规划算法php,php算法学习之动态