Created
December 6, 2017 07:58
-
-
Save aayushdrolia/2605c96907f7d1d59841ca03e01b0e05 to your computer and use it in GitHub Desktop.
Allow only numbers in text field
This file contains hidden or 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
$(document).ready(function () { | |
$("#field_id").keydown(function (e) { | |
if (e.shiftKey) e.preventDefault(); | |
else { | |
var nKeyCode = e.keyCode; | |
//Ignore Backspace and Tab keys | |
if (nKeyCode == 8 || nKeyCode == 9) return; | |
if (nKeyCode < 95) { | |
if (nKeyCode < 48 || nKeyCode > 57) e.preventDefault(); | |
} else { | |
if (nKeyCode < 96 || nKeyCode > 105) e.preventDefault(); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment