Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Text box accept only multiple on 0.25 values.

1. ## Text box accept only multiple on 0.25 values.

hi,
i got a problem.i have a text box. it should accept 0.25 multiples means 0.25,0.50,0.75,1.00,1.25,1.50 like that.

i have got requirement like this.

15 Minutes - 0.25
30 Minutes - 0.50
45 Minutes - 0.75
60 Minutes - 1.00
75 Minutes - 1.25 and so on...

can any one help me on this

2. Try this:-

Code:
<form>
<input name="inp1" id = "inp1"size = "12" maxlength = "12"  onkeyup="only2DP(this)" onblur = "checkcents(this)">
</form>

<script type = "text/javascript">

function only2DP(which){

var str = which.value;
str = str.replace(/[^0-9\.]/g,"");
var pos = str.indexOf(".");
if (pos > -1) {
str = str.substring(0, pos+3);
}
which.value = str;

}

function checkcents(which) {

var str = which.value;
var pos = str.indexOf(".");
if (pos == -1) {  // no decimal point
str = str + ".00";
which.value = str;
}
var pos = str.indexOf(".");
var cents = str.substring(pos+1);
if ((cents!="00") && (cents!=25) && (cents!=50) && (cents !=75)) {
alert ("Invalid entry - you may only enter decimals 00, 25, 50 or 75");
var dollars = str.substring(0,pos+1);
which.value = dollars;
return false;
}

}

</script>
The fool who knows his foolishness, is wise at least so far. But a fool who thinks himself wise, he is called a fool indeed.

3. ## Thak you soooo much

it is working..
Thank you so much your help. and this is resolve my exact requirement

Thank you Philip..

4. Originally Posted by kanna443
it is working..
Thank you so much your help. and this is resolve my exact requirement

Thank you Philip..
Note that I have slightly revised/improved the script.

You may of course name the variables hours and minutes rather than dollars and cents.

5. 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">
<meta charset= "utf-8">
<title>25 cent</title>
<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>

6. Originally Posted by mrhoo
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">
<meta charset= "utf-8">
<title>25 cent</title>
<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;

7. ## Users who have thanked Philip M for this post:

mrhoo (03-14-2013)

8. 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

9. ## time conversion from my total time

Thank you

0.25 -15 Minutes
0.50 -30 Minutes
0.75 -45 Minutes
1.00 -60 Minutes
1.25 -75 Minutes

as per above scenario I got total time as 100.25 in my text field. i have to convert as no of Day(s) no of hour(s) no of Minute(s)

can any one help on this

Thank you

10. Originally Posted by kanna443
Thank you

0.25 -15 Minutes
0.50 -30 Minutes
0.75 -45 Minutes
1.00 -60 Minutes
1.25 -75 Minutes

as per above scenario I got total time as 100.25 in my text field. i have to convert as no of Day(s) no of hour(s) no of Minute(s)

can any one help on this

Thank you
Hmmm. I get the idea that you ought to be able to code this simple conversion yourself. Did you even try?

Code:
<script type = "text/javascript">

var hours = 108.75;
var days = Math.floor(hours/24);
hrs = Math.floor(hours%24);
var decimal = hours - Math.floor(hours);
var minutes = Math.round(decimal * 60);

alert (hours + " hours is equal to " + days + " days " + hrs + " hours " + minutes + " minutes")

</script>

11. thank you vary much..

12. philp i got new issue.

i am retuning this blow value (through ID) and i have displayed in text box in my form.
but i need t dispaly as lable.can you help me

document.getElementById("actualDays").value= +days+" Day(s) "+hrs+" Hour(s) "+minutes+" Minute(s)";

my text box <input type="text" name="actualDays" id="actualDays" value="" size="30" />

13. Originally Posted by kanna443
philp i got new issue.

i am retuning this blow value (through ID) and i have displayed in text box in my form.
but i need t dispaly as lable.can you help me

document.getElementById("actualDays").value= +days+" Day(s) "+hrs+" Hour(s) "+minutes+" Minute(s)";

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"?

14. Originally Posted by Philip M
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

15. Originally Posted by kanna443
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.

16. ## Replay

sorry sorry Philip i am extremely sorry. i am promising you now onwards i will take care of my spelling mistake and i will try to resolve the issues

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•