How can I restrict input to a text-box so that it accepts only numbers and the decimal point?
+3
A:
form.onsubmit = function(){
if(textarea.value.match(/^\d+(\.\d+)?$/)) return true;
else return false;
}
Is this what you're looking for?
I hope it helps.
EDIT: I edited my example above so that there can only be one period, preceded by at least one digit and followed by at least one digit.
tau
2010-05-11 05:06:28
Think this will also validate '99.23.65.86' ....But i guess the question was about validating '56987.32' with single dot.....
SpikETidE
2010-05-11 05:23:44
i see the poster has since edited his/her original question. thanks for the update!
tau
2010-05-11 05:25:56
Try with ^\d+(\.\d+)?$
Ben
2010-05-11 05:35:21
@ben: thanks, that is even better.
tau
2010-05-11 05:55:22
+1
A:
Google a bit for 'Text Box Validation with Javascript'....
You will get tons of ideas....
Also see this other Stack overflow question
SpikETidE
2010-05-11 05:10:30
+1
A:
Are you looking for something like this?
<HTML>
<HEAD>
<SCRIPT language=Javascript>
<!--
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
</BODY>
</HTML>
lak-b
2010-05-11 05:20:09
+1
A:
inputelement.onchange= inputelement.onkeyup= function isnumber(e){
e= window.event? e.srcElement: e.target;
while(e.value && parseFloat(e.value)+''!= e.value){
e.value= e.value.slice(0, -1);
}
}
kennebec
2010-05-11 05:30:04
A:
how can you restrict number inputs only on your text box? what i mean is you cannot input non numeric inputs on your text box, how can i do that?
rebisco
2010-05-13 06:29:19
A:
31 && (charCode < 48 || charCode > 57)) return false;
return true;
}
//-->
This really works!
rebisco
2010-05-13 06:52:29