Disable only .(DOT) special charecter in input field

15.7k Views Asked by At

I don't want allow user to enter. (Dot) in my input field.. Rest of the required validation done already.

function isNumberKey(evt) {
  var charCode = (evt.which) ? evt.which : event.keyCode;
  if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
    return false;

  return true;
}
<input type="text" class="form-control" name="ContactNumber" maxlength="10" value="" id="ContactNumber" title="Enter Number Here" onkeypress="return isNumberKey(event)" required>

2

There are 2 best solutions below

2
On BEST ANSWER

$("#name").on("keypress", function(evt) {
  var keycode = evt.charCode || evt.keyCode;
  if (keycode == 46) {
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="name" />

Working example here: jsfiddle

3
On

In jQuery:

jQuery.validator.addMethod("someRule", function(string, element) { 
    return !string.match(/\./g);
}, "Your username contains a dot!");

In HTML use the pattern attribute:

pattern="[^.]"