I am new for JavaScript can you please suggest me how to validate float value on key press event. I have tried a example it is taking two '.'
Asked
Active
Viewed 3,981 times
0
-
1You should post a sample of what you are trying to do and what you have tried. – gpojd Jan 17 '13 at 18:42
-
you might want to google "javascript parse float". – Denys Séguret Jan 17 '13 at 18:43
-
http://stackoverflow.com/questions/3885817/how-to-check-if-a-number-is-float-or-integer – sdespont Jan 17 '13 at 18:44
-
hi check the answer its working for me ...will work for you also...i hope that – Pranay Rana Jan 17 '13 at 18:49
-
@user1978104 - dont forget to upvote and mark answer as accepted if it work for you.. – Pranay Rana Jan 17 '13 at 19:16
1 Answers
1
here is the code for you , this function can be used for integer as well as decimal
<input id="MyTextBox1" onkeypress=' IntegerAndDecimal(event,this,true)' >
function IntegerAndDecimal(e,obj,isDecimal)
{
if ([e.keyCode||e.which]==8) //this is to allow backspace
return true;
if ([e.keyCode||e.which]==46) //this is to allow decimal point
{
if(isDecimal=='true')
{
var val = obj.value;
if(val.indexOf(".") > -1)
{
e.returnValue = false;
return false;
}
return true;
}
else
{
e.returnValue = false;
return false;
}
}
if ([e.keyCode||e.which] < 48 || [e.keyCode||e.which] > 57)
e.preventDefault? e.preventDefault() : e.returnValue = false;
}

Pranay Rana
- 175,020
- 35
- 237
- 263