You need to sign in to do that
Don't have an account?
Subha Ayyappan 7
Javascript Function for KeyPress
Hi, I need a help. Below javascript function will not allow to enter more than 6 numbers or 4 numbers+'.'+1 number.
But if I have the number as 1234.5 and I delete 1234 then the function is letting me to enter .55555 which shud not happen.
I am not able to find where i went wrong. Can anybody help please?
function OnKeyPress(e,DivID) {
if ( e.which != 8 && e.which != 0 && e.which != 13 && e.which != 46 && (e.which < 48 || e.which > 57)) {
return false;
}
var val = j$('[id$='+DivID+']').val();
if(DivID == 'ProximityCPPercentage')
{
var x = event.which || event.keyCode;
if(val.indexOf('.') >= 0 && e.which == 46)
return false;
else if(e.which == 46 && val.length == 3)
return false;
if(val.indexOf('.') == 0)
val = '0' + val;
if(e.which != 46)
{
strval = val + String.fromCharCode(x);
var re = /^((.|0|[1-9]\d?)(\.\d{1})?|100(\.0?)?)$/;
if(!re.test(strval))
return false;
}
}
else if(val.indexOf('.') > 0)
{
if(e.which == 46 )
return false;
var arra = val.split('.');
var decval = arra[1];
var val = arra[0];
if(val.length > 6)
return false;
if(decval.length > 0)
return false;
}
else if(e.which != 46 )
{
if(val.length > 5)
return false;
}
}
But if I have the number as 1234.5 and I delete 1234 then the function is letting me to enter .55555 which shud not happen.
I am not able to find where i went wrong. Can anybody help please?
function OnKeyPress(e,DivID) {
if ( e.which != 8 && e.which != 0 && e.which != 13 && e.which != 46 && (e.which < 48 || e.which > 57)) {
return false;
}
var val = j$('[id$='+DivID+']').val();
if(DivID == 'ProximityCPPercentage')
{
var x = event.which || event.keyCode;
if(val.indexOf('.') >= 0 && e.which == 46)
return false;
else if(e.which == 46 && val.length == 3)
return false;
if(val.indexOf('.') == 0)
val = '0' + val;
if(e.which != 46)
{
strval = val + String.fromCharCode(x);
var re = /^((.|0|[1-9]\d?)(\.\d{1})?|100(\.0?)?)$/;
if(!re.test(strval))
return false;
}
}
else if(val.indexOf('.') > 0)
{
if(e.which == 46 )
return false;
var arra = val.split('.');
var decval = arra[1];
var val = arra[0];
if(val.length > 6)
return false;
if(decval.length > 0)
return false;
}
else if(e.which != 46 )
{
if(val.length > 5)
return false;
}
}