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

eval question

Status
Not open for further replies.

tankim

Programmer
Joined
Sep 11, 2003
Messages
4
Location
US
I have an html form that has several textboxes (<input type=&quot;text&quot;). What I want to do is to allow users to enter numbers and number equations like +, -, /, and * into the textbox, and display the result on the textbox. For example, user will enter &quot;200 / 4&quot; and the form will display &quot;50&quot;.

When onblur event happens, I do an eval on the content of the textbox, forcing the calculation of the contents of the textbox. I then replace the contents of the textbox with the result from eval. It works fine if I enter &quot;200 / 4&quot; or &quot;200&quot;. &quot;200 / 4&quot; gives me &quot;50&quot;, &quot;200&quot; gives me &quot;200&quot;. However, when I enter &quot;0200&quot;, I am getting &quot;128&quot;. It seems like the problem happens if the number entered starts with &quot;0&quot;.

Could someone englighten me on this?

Thanks in advance.
 
It assumes the number is a hexadecimal when it starts with a &quot;0&quot; and you eval or parseInt it...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Thanks.

Do you have a suggestion on how to get around this?
 
Check that last post - numbers starting with a zero are seen as octals(base-8) values - so long as the next digit (after the inital 0) is between 0 and 7.

If you were just using parseInt, you could add a radix arguement(10) to let it know that you are working with decimal numbers - that isn't possible with the eval() function...

The only solution I see is to pull the string apart... yuck... You may want to see if the first number is a zero and alert the user...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Thanks.

I think I will try to use regular expression to strip off leading zeroes. See how much success I have on this approach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top