I have to allow user to allow only numeric values in the text box . It should remove any non numeric method. No matter how they are supplying the input …either typing or copy+paste.
My code
var nCheck = function (field) {
var re = /^[0-9]*$/;
if (!re.test(field.value)) {
field.value = field.value.replace(/[^0-9]*$/g,"");
}
};
This works except when you copy paste something like aaa2 . Whereas aaaa or aa work. Problem is when last place contains number it fails .
Thanks for any help.
The
$means “end of string”. Remove it and it should work.Other tips:
Replace
*by+;*includes the empty stringMaybe it should better to warn the user, just in case the numeric character is a typo (like pressing Q instead of 1)