Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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.
    Last edited by Philip M; 03-14-2013 at 06:33 PM. Reason: Improved

    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.

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thak you soooo much

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

    Thank you Philip..

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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.
    Last edited by Philip M; 03-14-2013 at 06:34 PM.

    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.

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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 06:58 PM. Reason: thanks Philip

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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;
    Last edited by Philip M; 03-14-2013 at 06:51 PM.

    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.

  • Users who have thanked Philip M for this post:

    mrhoo (03-14-2013)

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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.

  • #8
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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 08:06 AM. Reason: more detail

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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>
    Last edited by Philip M; 03-15-2013 at 09:56 AM.

    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.

  • #10
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you vary much..

  • #11
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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" />

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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.

  • #13
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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.

  • #15
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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 LastLast

    Posting Permissions

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