Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jeffrey Phillips Freeman
agusmakmun-github-io
Commits
7c8f4d4e
Commit
7c8f4d4e
authored
Apr 26, 2016
by
agusmakmun
Browse files
Add post
parent
f27ae158
Changes
1
Hide whitespace changes
Inline
Side-by-side
_posts/2016-04-26-javascript-validator-for-input-number.markdown
View file @
7c8f4d4e
...
...
@@ -6,23 +6,6 @@ categories: [others]
---
This javascript will validate/allow the number only when event key is pressed.
{% highlight html %}
<input
id=
"id_price"
type=
"number"
min=
0
/>
<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;
}
document.getElementById('id_price').setAttribute("onkeypress", "return isNumber(event)");
</script>
{% endhighlight %}
For example result of it:
<iframe
width=
"100%"
height=
"300"
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>
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment