Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-14-2013, 01:43 PM   PM User | #1
kanna443
New Coder

 
Join Date: Mar 2013
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
kanna443 is an unknown quantity at this point
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
kanna443 is offline   Reply With Quote
Old 03-14-2013, 04:09 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 03-14-2013 at 05:33 PM.. Reason: Improved
Philip M is offline   Reply With Quote
Old 03-14-2013, 04:25 PM   PM User | #3
kanna443
New Coder

 
Join Date: Mar 2013
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
kanna443 is an unknown quantity at this point
Thak you soooo much

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

Thank you Philip..
kanna443 is offline   Reply With Quote
Old 03-14-2013, 05:31 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by kanna443 View Post
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.
__________________

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.

Last edited by Philip M; 03-14-2013 at 05:34 PM..
Philip M is offline   Reply With Quote
Old 03-14-2013, 05:34 PM   PM User | #5
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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
mrhoo is offline   Reply With Quote
Old 03-14-2013, 05:40 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by mrhoo View Post
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.

Last edited by Philip M; 03-14-2013 at 05:51 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
mrhoo (03-14-2013)
Old 03-14-2013, 06:26 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 03-15-2013, 06:59 AM   PM User | #8
kanna443
New Coder

 
Join Date: Mar 2013
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
kanna443 is an unknown quantity at this point
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

Last edited by kanna443; 03-15-2013 at 07:06 AM.. Reason: more detail
kanna443 is offline   Reply With Quote
Old 03-15-2013, 08:47 AM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by kanna443 View Post
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>
__________________

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.

Last edited by Philip M; 03-15-2013 at 08:56 AM..
Philip M is offline   Reply With Quote
Old 03-15-2013, 11:33 AM   PM User | #10
kanna443
New Coder

 
Join Date: Mar 2013
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
kanna443 is an unknown quantity at this point
thank you vary much..
kanna443 is offline   Reply With Quote
Old 03-15-2013, 11:45 AM   PM User | #11
kanna443
New Coder

 
Join Date: Mar 2013
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
kanna443 is an unknown quantity at this point
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" />
kanna443 is offline   Reply With Quote
Old 03-15-2013, 12:05 PM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by kanna443 View Post
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"?
__________________

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.
Philip M is offline   Reply With Quote
Old 03-15-2013, 12:23 PM   PM User | #13
kanna443
New Coder

 
Join Date: Mar 2013
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
kanna443 is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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
kanna443 is offline   Reply With Quote
Old 03-15-2013, 01:26 PM   PM User | #14
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by kanna443 View Post
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.
Philip M is offline   Reply With Quote
Old 03-15-2013, 01:37 PM   PM User | #15
kanna443
New Coder

 
Join Date: Mar 2013
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
kanna443 is an unknown quantity at this point
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
kanna443 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:54 PM.


Advertisement
Log in to turn off these ads.