diff --git a/_posts/2016-04-26-javascript-validator-for-input-number.markdown b/_posts/2016-04-26-javascript-validator-for-input-number.markdown index 0c04c16ba0a6e9a406f07a3f06a804ee30a0750b..f7c7a141b44b334a932a85e20394b8f62dfe998d 100644 --- a/_posts/2016-04-26-javascript-validator-for-input-number.markdown +++ b/_posts/2016-04-26-javascript-validator-for-input-number.markdown @@ -8,4 +8,23 @@ categories: [others] This javascript will validate/allow the number only when event key is pressed. For example result of it: -<iframe width="100%" height="350" src="//jsfiddle.net/agaust/3qz105nn/embedded/html,result/dark/" allowfullscreen="allowfullscreen" frameborder="0"></iframe> \ No newline at end of file +<iframe width="100%" height="350" src="//jsfiddle.net/agaust/3qz105nn/embedded/html,result/dark/" allowfullscreen="allowfullscreen" frameborder="0"></iframe> + +tutorial above, when you can not directly add the attribute inside html tag. +but if you can add it, you can following this bellow: + +{% highlight html %} +<input id="id_price" type="number" min=0 onkeypress="return isNumber(event)"/> +<script type="text/javascript"> +function isNumber(evt) { + evt = (evt) ? evt : window.event; + var charCode = (evt.which) ? evt.which : evt.keyCode; + if (charCode > 31 && (charCode < 48 || charCode > 57)) { + return false; + } + return true; +} +</script> +{% endhighlight %} + +hope it usefull. \ No newline at end of file