I have the following function attached to the onkeypress event of a textbox. It only allows one integer after decimal point numbers(for eg:allow .2,1.2,12.3, not allow 2.34,.24,..,2..3,) and integers to be entered into the textbox.I want to prevent pasting alphabets special characters etc.using the following code Restricting input to textbox: allowing only numbers and decimal point,but i have no idea to avoid pasting into a textbox prevention
However users can still paste invalid text into it. What is the best way to stop this??
onkeypress="return isNumberKey(this,event);"
function isNumberKey(obj,evt) {
var charCode = (evt.charCode) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
if (window.event) //IE
{
window.event.returnValue = false;
}
else //Firefox
{
return false; // <-- What should I write here instead
}
else {
var input = obj.value;//alert(input);
var len = obj.value.length; // alert(len);
var index = obj.value.indexOf('.'); // alert(index);
if (index > 0 && charCode == 46) {
if (window.event)
{
window.event.returnValue = false;
}
else
{
return false;
}
}
if (index >0 || index==0) {
var CharAfterdot = (len + 1) - index;
if (CharAfterdot > 2) {
if (window.event)
{
window.event.returnValue = false;
}
else
{
return false;
}
}
}
if (charCode == 46 && input.split('.').length >1) {
if (window.event)
{
window.event.returnValue = false;
}
else
{
return false;
}
}`enter code here`
}
return true;
}