Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

keep text field from auto inserting comma after 3rd number. 1

Status
Not open for further replies.

CMcC

Programmer
Feb 5, 2002
196
US
Hello all. I have a form with a text field that expects the users to enter a currency value.
I would like the field to contain NO COMMA's - as it messes up the query_string that is submited by the form. I tried:

Code:
function removeCommas(str) {
   return str.replace(/,/g, "");
}

and the specific textfield code:
Code:
 <input name="amount" id="amount" 
      size="35"  onkeyup="removeCommas(this);format(this);" onchange="removeCommas(this);format(this);"/>

but if the form doesnt validate all fields, it reverts back with the commas in this field.

the format function makes sure the user enters more than 2 numbers after the decimal. Now want to make sure a number such as:

12,234.88 goes to the query_string as 12234.88

Using Dreamweaver 8.

Any ideas? all help is appreciated!
cmcc
 
What server side language are you using to send the query?

you should be able to take the value form the textbox and modify it as you need before plugging it in the query.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
hello vacunita.
Using javascript to modify the value - but I see that when I call the two functions in the onchange and keyup event - only the first function is performed.

Code:
 <script type="text/javascript">
function format(input){
var num = input.value.replace(/\,/g,'');
if(!isNaN(num)){
if(num.indexOf('.') > -1){ 
num = num.split('.');
num[0] = num[0].toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
if(num[1].length > 2){ 
alert('You may only enter two decimals!');
num[1] = num[1].substring(0,num[1].length-1);
} input.value = num[0]+'.'+num[1]; 
} else{ input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'') };
}
else{ alert('You may enter only numbers in this field!');
input.value = input.value.substring(0,input.value.length-1);
}
}
</script>
<script type="text/javascript">

function removeCommas(str) {
   return str.replace(/,/g, "");
}
</script>

ie - only the commas are removed, but it doesnt check that there are only 2 digits after decimal or that all info is numeric.
 
In that case i would like to point you to the Javascript forum: forum216.

If you can;t run both, can you call a function that then calls the other 2? I may be grasping here, since i have limited knowledge of Javascript. but i would guess you can make a function that can call the other 2. and call that one instead on your keyup event.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I so wish there was en edit function:
forum216



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for your help. I have called 2 functions from one and it only runs the first function. Maybe it is the function itself that is incorrect.
cmcc
 
The guys over at the JS forum will be able to help.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top