简体   繁体   中英

Display currency symbol ($) in an asp.net textbox using JavaScript

I am using asp.net textbox

I want to display the value entered by user in a proper currency format eg if user entered 10000 and I want it to be formatted like this: 10,000 $

How this could be achived via javascript without using ajaxtool kit?

You could achieve this with javascript. The following code will change the value as the user types:

javascript:

   function fnCurrencyOnly(o) {
        var sValue = o.value;
        var sKey = String.fromCharCode(window.event.keyCode);
        if (document.selection.createRange().text == sValue) {
            sValue = sKey;
        } else {
            sValue = sValue + sKey;
        }
        var re = new RegExp("^\\d+(?:\\.\\d{0,2})?$");
        if (!sValue.match(re))
            window.event.returnValue = false;
    }

html:

<div>
   <asp:textbox runat="Server" ID="text" onchange="fnCurrencyOnly(this);" />
</div>

Try something like this

    function formatText(txt)
    { 
 txt.value = '$' + txt.value;

    }

<asp:textbox onchange="javascript:formatText(this);"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM