YVS1102 YVS1102 - 1 year ago 88
Javascript Question

Disable input dot '.' javascript

I'm trying to disable user for input dot

. Here is what i try so far

$('#num').on('input', function (event) {
this.value = this.value.replace(/^[0-9]+\.?[0-9]*$//./, '');

with my script above, i still can input dot
. Is there anyway to prevent user to input dot

thanks in advance, and sorry for my bad engllush

Answer Source

Check the keyCode on the keydown event and do a preventDefault if it's 190 (the period key). Test below with "Run code snippet".

$('#num').keydown(function(e) {
  if (e.keyCode === 190) {
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="num">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download