Last active
August 29, 2015 14:08
-
-
Save bni/384978bce0694e155362 to your computer and use it in GitHub Desktop.
jQuery numeric plugin, allow both "," and "."
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="http://code.jquery.com/jquery-1.9.0.js"></script> | |
<script src="https://raw.githubusercontent.com/SamWM/jQuery-Plugins/master/numeric/jquery.numeric.js"></script> | |
<label for="integer-input">Integer</label> | |
<input id="integer-input" type="number" step="1" min="0" class="integer-input" value=""> | |
<label for="numeric-input">Numeric</label> | |
<input id="numeric-input" type="text" class="numeric-input" value="" onkeydown="setInputFormat(event);"> | |
<script> | |
$("input.integer-input").numeric({decimal: false, negative : false}); | |
$("input.numeric-input").numeric({decimal: ".", negative : false}); | |
// Workaround to allow both "," and "." | |
// jQuery numeric plugin cant handle two separators at the same time | |
// So we have to set it depending on what key was pressed | |
function setInputFormat(e) { | |
if (e.keyCode == 188) { // "," | |
$("input.numeric-input").numeric({decimal: ",", negative : false}); | |
} else if (e.keyCode == 190) { // "." | |
$("input.numeric-input").numeric({decimal: ".", negative : false}); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment