Philip has an excellent method, but the original problem made me think of how to round the input to the next higher multiple of 25 cents, rather than removing it.
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>25 cent</title>
</head>
<body>
<form>
<input name="inp1" type="text" onchange= "checkcents(this)">
</form>
<script type = "text/javascript">
function checkcents(which){
var n= +which.value || 0,
n2= Math.ceil(n/.25)*.25;
if(n!= n2){
alert("you may only enter decimals 00, 25, 50 or 75");
which.focus();
}
which.value= n2.toFixed(2);
}
</script>
</body>
</html>
Last edited by mrhoo; 03-14-2013 at 05:58 PM..
Reason: thanks Philip
Philip has an excellent method, but the original problem made me think of how to round the input to the next higher multiple of 25 cents, rather than removing it.
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>25 cent</title>
</head>
<body>
<form>
<input name="inp1" type="text" onchange= "checkcents(this)">
</form>
<script type = "text/javascript">
function checkcents(which){
var n= +which.value,
n2= Math.ceil(n/.25)*.25;
if(n!= n2){
alert("you may only enter decimals 00, 25, 50 or 75");
which.focus();
}
which.value= n2.toFixed(2);
}
</script>
</body>
</html>
I thought of that, but it is an assumption that the user desires to round his entry up. He might want to round .1 down to .00.
Your script does not trap NaN entries - suggest
var n= Number(which.value) || 0;
which.value = n;
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
For the record, you can use this to round a number to (say) the nearest .25
Code:
<script type = "text/javascript">
var num =1.57; // number to round to nearest multiple of .25
var rf = .25; // rounding factor
var result = Math.round(num/rf)*rf;
alert (result.toFixed(2)); // result is 1.50
</script>
Math.floor rounds down to the nearest .25.
Math.ceil rounds up to the nearest .25
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
my text box <input type="text" name="actualDays" id="actualDays" value="" size="30" />
Make an effort to solve this yourself. If you are really stuck come back and show your code. Please check your posts for typos. What is a "blow value"?
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Make an effort to solve this yourself. If you are really stuck come back and show your code. Please check your posts for typos. What is a "blow value"?
oh sorry for wrong spelling.i ma solving this thank you
oh sorry for wrong spelling.i ma solving this thank you
Ma you? It is always best to try to develop a solution yourself before seeking help in this forum. If you cannot be troubled to get your spelling right, or people's names, then you ought not to ask other people to take time and trouble to help you.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.