How do i round up to two decimal places

hello guys,
i am not good at javascript, but trying to implement or google up a function that would only allow characters in a textbox to have

  1. only numeric characters
  2. 2 decimal places.

I got this online, but it slices the 3rd decimal, i want it to allow only 2 decimals, any pointers ? help ?

thanks
Ehi

I’m not sure exactly what you’re looking for. Are you looking to run a function that only shows certain (numerical) characters when a user is typing into a textbox? … or are you just looking for a general way to convert a number to two decimal places?

What’s wrong with number.toFixed(2) ?

Doing this as people type becomes very tricky, due to cross-browser differences in handling key events. For a good run-down of the problems, see detecting keystrokes

So instead of doing it on a keystroke basis, it’s commonly more stable to do it after the field has been filled in, or when the form is submitted.

Here is a sample form field


<form id="someForm">
    <p>
        <label for="amount">Amount:</label>
        <input id="amount" type="text" name="amount">
    </p>
</form>

And here is the script that updates the field when you move out of the field. The script that is placed just before the </body> tag.


var form = document.getElementById('someForm');
form.elements.amount.onblur = dollarHandler;

function dollarHandler() {
    var number = Number(this.value) || 0;
    this.value = number.toFixed(2);
}

Note: ‘someForm’ is the identifier of the form, and form.elements.amount is the name attribute of the form field.

thanks for your suggestion, but am working with telerik controls in a web control file, .asax file and it doesnt have a web form attached to it.

Is there another way around it ?

I have no experience with telerik controls in a .asax web control file, but no doubt someone in the .NET forum will be able to piece that together for you.

Ok thanks.

Its a javascript problem, and not .net so no point posting it there.

thank you

So how do we resolve the telerik controls in a .asax web control file - that isn’t something that the javascript forum tends to deals with.

Telerick controls and asax files are simply .net files, with enhanced functionality.

The only problem here, is they dont have a webform, which normally pages do have, apart from that, it works the same way, cos the above javascript and other javascript funtions work ok.

Its just no form tags

You shouldn’t have a textbox without the form tags . . .

Yeah, maybe you should throw more light into what you want to achieve, if what you are working with has no form tags, then where’s the textbox going to be exactly…

We are working on a project, that uses telerik controls. And they have the web controls *.asax files in the webforms *.aspx files, which dont have the <form><\form> tags.

I was surprised initially, but was told thats how telerik controls work.

But at runtime, i noticed the page, has this

<form name=“aspnetForm” method=“post” action=“record.aspx” id=“aspnetForm”>

maybe i would try the javascript using the above name which appears at runtime, but not design mode.