Page 1 of 1
cents in text input
Posted: Wed Jul 14, 2010 11:21 am
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
Re: cents in text input
Posted: Wed Jul 14, 2010 12:03 pm
by JakeJ
If ($x > 1) {
$x = $x * .01;
}
Is that what you need?
Re: cents in text input
Posted: Wed Jul 14, 2010 12:12 pm
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
Re: cents in text input
Posted: Wed Jul 14, 2010 1:43 pm
by John Cartwright
Re: cents in text input
Posted: Wed Jul 14, 2010 3:02 pm
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'>
Re: cents in text input
Posted: Wed Jul 14, 2010 5:54 pm
by Weirdan
This is in fact an interesting challenge. My take:
http://jsfiddle.net/V9XnF/
Re: cents in text input
Posted: Wed Jul 14, 2010 6:38 pm
by JakeJ
I'm not quite sure what I'm supposed to do with that Weirdan.
Re: cents in text input
Posted: Thu Jul 15, 2010 3:33 am
by VladSun
Very nice

It doesn't work with Opera 10.60 for me - I can't enter any input (but Tab is working)