I don’t know why I’m having so much trouble searching this, it seems really easy, but I can’t find any information.
Basically I want a control on my ASP.net form to be updated depending on what number is in a field.
So if I had 0x0= it would show 0, and if i changed the number to 5×5= it would automatically display 25 (as soon as I finished typing it)
Edit: I don’t really have a good code sample, here is an example of what I want.
<asp:textbox></asp:textbox>X<asp:textbox></asp:textbox> = <asp:label text="value of the last two being changed dynamically" />
Using
try..catchwitheval()I’ve created an unsafe example on JSFiddle that does what you require but instead of waiting for result when caret is still on the input box, I attached to
blurevent so value gets evaluated when you switch focus outside of the control.What it does is this:
It tries to evaluate content of the input box and if it was able to do so, replace content with the result. This means that you can throw anything at it as long as
evalcan chew on it.I said unsafe, because code’s not checking content for anything that could cause black holes to swallow universes… 😉
Examples
1 + 2 * 3will change 7universewill leave it as universe010+1will change to 9 (so it groks octals as well)0xf *2 +3will return 33 (look ma’ hex as well)Implement your way to make it safer
This is of course just a start. If you’d like x to be become multiplicator, you will have to do some replacements in input string, that my example doesn’t do. But it’s pretty straight forwards to do so.
The same is true if you’d only like to do particular calculations, so you don’t allow creating objects or dates or anything…
An example that is likely undesirable in your case is if you’d type in
which would of course execute this code and display an alert box. Whether you’d like to allow this or not is on you. If you’d be posting original data to server yu’d of course want to sanitize inputs severely.