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.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    cents in text input

    hi folks, i needed a text input so the user that will register a product, will make sure to enter the cents, and not just the raw money, making it to be stored incorrectly on my database.

    for example, if i type "1" the output inside the text field will be automatically "0.01", so the user HAS to type the cents.

    thanks in advance

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    What's your question?
    Are you a Help Vampire?

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    well my question is how to make it... how to program a javascript function that updates the typed value to cents on key down. again, if i type 1, it will display 0.01 on the text field, if i type 100, it will display 1.00
    thanks in advance

  • #4
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    well my question is how to make it... how to program a javascript function that updates the typed value to cents on key down. again, if i type 1, it will display 0.01 on the text field, if i type 100, it will display 1.00
    thanks in advance

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    who.value= (who.value/100).toFixed(2);

    <!doctype html>
    <html lang= "en">
    <head>
    <meta charset= "utf-8">
    <title> dollars and cents</title>
    <script>
    Code:
    function formatCents(e){
    	var who, val;
    	e= window.event || e;
    	who= e.srcElement || e.target;
    	if(who){
    		val= who.value.replace(/\D+/g,'');
    		if(isNaN(+val)){
    			who.value= '0.00';
    			who.select();
    		}
    		else who.value= (val/100).toFixed(2);
    		if(e.stopPropagation) e.stopPropagation();
    		e.cancelBubble= true;
    	}
    }
    onload= function(){
    	var who= document.getElementsByName('dollarcents')[0], F= who.form;
    	F.onsubmit= function(){alert(parseFloat(who.value).toFixed(2))}
    	who.onkeyup=formatCents;
    	who.select();
    }
    </script>
    </head>

    <body>
    <form action= "" method= "post">
    <fieldset>
    <label style= "text-align:right;font-size:x-large"> Enter amount: $
    Code:
    <input size= "11" name= "dollarcents" type= "text" value= "0.00" 
    maxLength="11" style= "text-align:left;font-size:x-large">
    </label>
    </fieldset>
    </form>
    </body>
    </html>
    Last edited by mrhoo; 07-14-2010 at 05:26 PM. Reason: formatting

  • #6
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks, thats what i need, and it works!

    just some thoughts:
    -onkeydown would be perfect, but changing it, will return 3 digits after the decimal, since it updates after the next key down, and that might add another digit, and that way, it will always have 3 decimals
    -if i type a number in front of it, it will simply add to the existing 0.00 so if i type number 1 AFTER the 0.00 it will work, but if i type 1 before, it will be 10.00, and i wanted to add digits to the first decimal regardless of where i type.

    is that possible?

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    The input value does not change at onkeydown, it changes onkeyup.

  • #8
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i know, but changing on key up gives me unwanted delay, thats why i asked for on key down


  •  

    Posting Permissions

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