Skip to content

Instantly share code, notes, and snippets.

@bni
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bni/384978bce0694e155362 to your computer and use it in GitHub Desktop.
Save bni/384978bce0694e155362 to your computer and use it in GitHub Desktop.
jQuery numeric plugin, allow both "," and "."
<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