I have this code in jquery to prevent non-numeric characters being inputted to the text field
$("#NumericField").numeric();
Now, on the text field i cant input non-numeric characters. That is OK. The problem here is if the user will paste on the text field with non numeric characters.
Is there a way/method to disable pasting if the value is non-numeric? Or is there any other approach to handle this situation that you can share?
jQuery isNumeric() method The isNumeric() method in jQuery is used to determine whether the passed argument is a numeric value or not. The isNumeric() method returns a Boolean value. If the given argument is a numeric value, the method returns true; otherwise, it returns false.
The standard solution to restrict a user to enter only numeric values is to use <input> elements of type number.
You can check if the user has entered only numbers using change event on input and regex. $(document). ready(function() { $('#myText').
you can use callback which checks on leaving field if value is valid if value is not valid then clear it and show error message:
var decimal_char = ',';
function isvalidnumber(){
var val=$(this).val();
//This regex is from the jquery.numeric plugin itself
var re=new RegExp("^\\d+$|\\d*" + decimal_char + "\\d+");
if(!re.exec(val)){
alert("Invalid number");
$(this).val("");
}
}
$(document).ready(function(){
$("#txtN").numeric(decimal_char,isvalidnumber);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With