mcxiand mcxiand - 2 years ago 82
jQuery Question

Validate numeric text field in jquery

I have this code in jquery to prevent non-numeric characters being inputted to the text field


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?

Answer Source

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+");
    	alert("Invalid number");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download