admin管理员组文章数量:1398226
I have this javascript code, which, on desktop browsers works well to ensure the user can only enter characters 1 to 9.
However when testing in Chrome on Android, the keypad presented includes dash and period characters which the field accepts. How can I prevent this?
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
UPDATE:
This is how I am using the function in HTML:
<input type="number" class="form-control"
onkeypress="return isNumberKey(event)" />
I have another function to prevent being able to copy and paste non numeric chars into the code.
$(document).ready(function() {
$('#number').bind("cut copy paste drag drop", function(e) {
e.preventDefault();
});
});
And if all else fails, there's server-side validation.
I really want to stop decimal places being possible on the front end. Remember the character range I specified restricts this fine on a qwerty keyboard - however on android (and probably others) the number key pad allows entering .
and -
I have this javascript code, which, on desktop browsers works well to ensure the user can only enter characters 1 to 9.
However when testing in Chrome on Android, the keypad presented includes dash and period characters which the field accepts. How can I prevent this?
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
UPDATE:
This is how I am using the function in HTML:
<input type="number" class="form-control"
onkeypress="return isNumberKey(event)" />
I have another function to prevent being able to copy and paste non numeric chars into the code.
$(document).ready(function() {
$('#number').bind("cut copy paste drag drop", function(e) {
e.preventDefault();
});
});
And if all else fails, there's server-side validation.
I really want to stop decimal places being possible on the front end. Remember the character range I specified restricts this fine on a qwerty keyboard - however on android (and probably others) the number key pad allows entering .
and -
- If all you want are numbers, why not explicitly check for the range of 48 - 57? Not sure why the 31 check is in there – Taplar Commented Dec 13, 2017 at 18:31
- Also can you show us what event binding(s) you are using this method in? – Taplar Commented Dec 13, 2017 at 18:33
- @Taplar Could it be to allow for backspace? I've updated my answer to give more detail – egmfrs Commented Dec 14, 2017 at 10:28
2 Answers
Reset to default 4One possible solution:
<script>
function onlyNumbers(num){
if ( /[^0-9]+/.test(num.value) ){
num.value = num.value.replace(/[^0-9]*/g,"")
}
}
</script>
<input type="text" onkeyup="onlyNumbers(this)">
Try using the HTML attributes min and max to set the range of numbers and type=number to allow for only numbers.
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
本文标签: javascriptPrevent decimal place being entered into input field on mobileStack Overflow
版权声明:本文标题:javascript - Prevent decimal place being entered into input field on mobile - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744115093a2591473.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论