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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strip commas from form based calculator?

Status
Not open for further replies.

cbsarge

IS-IT--Management
Jun 20, 2001
219
US
I have the below calculator and was hoping there was a way to strip out commas that users may inadvertently paste into it.

Thanks for any help!

Code:
 <form name="calculator"> 
 <table border="4" cellpadding="1" bordercolor="#FFFFFF" bgcolor="#73B27B" cellspacing="2" width="222"> 
 <tr> 
 <td> 
 <input type="text" size="25" length="25" value="" name="ans" style="background:beige;color:black;"> 
 </td> 
 </tr> 
 </table> 
 <table border="4" cellpadding="2" bordercolor="#FFFFFF" cellspacing="2" width="150" bgcolor="#73B27B"> 
 <tr> 
 <td align="center"><input type="button" value="  7  " name="seven" onClick="document.calculator.ans.value+='7'"></td> 
 <td align="center"><input type="button" value="  8  " name="eight" onClick="document.calculator.ans.value+='8'"></td> 
 <td align="center"><input type="button" value="  9  " name="nine" onClick="document.calculator.ans.value+='9'"></td> 
 <td align="center"><input type="button" value="  /  " name="divide" onClick="document.calculator.ans.value+='/'"></td> 
 </tr> 
 <tr> 
 <td align="center"><input type="button" value="  4  " name="four" onClick="document.calculator.ans.value+='4'"></td> 
 <td align="center"><input type="button" value="  5  " name="five" onClick="document.calculator.ans.value+='5'"></td> 
 <td align="center"><input type="button" value="  6  " name="six" onClick="document.calculator.ans.value+='6'"></td> 
 <td align="center"><input type="button" value="  *  " name="multiply" onClick="document.calculator.ans.value+='*'"></td> 
 </tr> 
 <tr> 
 <td align="center"><input type="button" value="  1  " name="one" onClick="document.calculator.ans.value+='1'"></td> 
 <td align="center"><input type="button" value="  2  " name="two" onClick="document.calculator.ans.value+='2'"></td> 
 <td align="center"><input type="button" value="  3  " name="three" onClick="document.calculator.ans.value+='3'"></td> 
 <td align="center"><input type="button" value="  -  " name="subtract" onClick="document.calculator.ans.value+='-'"></td> 
 </tr> 
 <tr> 
 <td align="center"><input type="button" value="  C  " name="clear" onClick="document.calculator.ans.value='"></td> 
 <td align="center"><input type="button" value="  0  " name="zero" onClick="document.calculator.ans.value+='0'"></td> 
 <td align="center"><input type="button" value="  =  " name="equal" onClick="document.calculator.ans.value=eval(document.calculator.ans.value)"></td> 
 <td align="center"><input type="button" value="  +  " name="add" onClick="document.calculator.ans.value+='+'"></td> 
 </tr> 
 </table> 
 </form>
 
Hi

Are you sure you want to remove only the commas ( , ) ? What if the user accidentally paste c, b, s, a, r, g or e ?

The simplest is to remove everything excepting the allowed characters :
Code:
[COLOR=darkgoldenrod]eval[/color][teal]([/teal]document[teal].[/teal]calculator[teal].[/teal]ans[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/[^0-9.+*/-]/[/fuchsia]g[teal],[/teal][green][i]'[/i][/green][teal]))[/teal]


Feherke.
 
So how do I use your code in the form? Is it only applied to the line containing the text element?

Thank you!
 
Hi

There is a single [tt]eval()[/tt] call in your code. Replace it with mine.

By the way, I consider your approach more hindering then helping. Personally I would call that an evaluator, not a calculator. As such, it would be able to perform more complex calculations using the [tt]Math[/tt] object's methods, for example [tt]Math.pow(2,3)/2[/tt] to calculate half of cube of 2. But eliminating the comma prevents that.


Feherke.
 
It's being used in the context of a page that has some dollar values with commas in them so the simple math is o.k.

Unfortunately making this change renders this error:

Uncaught SyntaxError: Unexpected token ILLEGAL

I'm using Chrome for the browser.

Code:
<td align="center"><input type="button" value="  =  " name="equal" onClick="document.calculator.ans.value=eval(document.calculator.ans.value.replace(/[^0-9.+*/-]/g,'))"></td>
 
Hi

Oops, I forgot about the Tek-Tips engines temporary illness. These days it removes one single quote ( ' ) where there are two one after another. ( Happened to your code too. See the "C" [tt]button[/tt]'s [tt]onclick[/tt] handler in your original post. ) Let me try again :
HTML:
<td align="center"><input type="button" value="  =  " name="equal" onClick="document.calculator.ans.value=eval(document.calculator.ans.value.replace(/[^0-9.+*/-]/g,''))"></td>
Explained in words, in case the above code gets distorted again :
Code:
replace(/[^0-9.+*/-]/g,``)
[gray]// two single quote ---^^--- characters here[/gray]
[gray]// ( here I wrote backticks ( ` ),[/gray]
[gray]//   replace them with single quotes ( ' ))[/gray]


Feherke.
 
That did it! Thanks so much for your help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top