cents in text input

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

cents in text input

Post by fael097 »

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
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: cents in text input

Post by JakeJ »

If ($x > 1) {
$x = $x * .01;
}

Is that what you need?
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

Re: cents in text input

Post by fael097 »

sort of... but i need to display it in real time inside a text field, you know?
im a php guy, not too much into JS, so idk how exactly to make it work, and display a dot automatically on the field and stuff
but your thing sounds like the right way, just needs some developing
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: cents in text input

Post by John Cartwright »

Code: Select all

number_format($input, 2); 
?
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: cents in text input

Post by JakeJ »

Javascript would be the way to go, I've just been learning it myself recently.
I can't guarantee this will work, but give it a shot.
Just change 'input' to whatever the id of the text box is that you want to change.
There's probably a more efficient way to to that, but oh well... I'm new to js too.

Try this:

Code: Select all

<html>
<head>
<script type='text/javascript'>
function calc() {
var num = document.getElementById('input').value
If(num > 1) {
  var result = num * .01
document.getElementById('input').value=result
}
}


</script>
</head>
<body>
<input type = button name = "button" onclick = "document.getElementById('input').value=calc()value = 'Click 2'>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: cents in text input

Post by Weirdan »

This is in fact an interesting challenge. My take: http://jsfiddle.net/V9XnF/
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: cents in text input

Post by JakeJ »

I'm not quite sure what I'm supposed to do with that Weirdan.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: cents in text input

Post by VladSun »

Weirdan wrote:This is in fact an interesting challenge. My take: http://jsfiddle.net/V9XnF/
Very nice :)
It doesn't work with Opera 10.60 for me - I can't enter any input (but Tab is working)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply