Implementation of thousand separator for a Textboxhttp://forums.asp.net/t/1616010.aspx/1?Implementation+of+thousand+separator+for+a+TextboxSat, 28 Apr 2012 18:05:03 -040016160104137801http://forums.asp.net/p/1616010/4137801.aspx/1?Implementation+of+thousand+separator+for+a+TextboxImplementation of thousand separator for a Textbox <p>I need a JavaScript code to implement the thousand separator for a Textbox on KeyUp event.</p> <p>Please help.</p> <p><br> </p> 2010-10-23T11:57:31-04:004137828http://forums.asp.net/p/1616010/4137828.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>I think that with&nbsp;the MaskedEdit extender of the Ajax Control Toolkit is enough for what you need.</p> <p>Check this URL: <a href="http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/MaskedEdit/MaskedEdit.aspx"> http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/MaskedEdit/MaskedEdit.aspx</a></p> <p>Hope this helps&nbsp;</p> 2010-10-23T12:51:36-04:004138094http://forums.asp.net/p/1616010/4138094.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Afshin_Zavar</h4> <p></p> <p>I need a JavaScript code to implement the thousand separator for a Textbox on KeyUp event.</p> <p>Please help.</p> <p><br> </p> <p></p> </blockquote> <br> <p></p> <p>Hi,</p> <p>I hope links bottom can guide you<br> </p> <p><a target="_blank" href="http://www.webmasterworld.com/forum91/8.htm">http://www.webmasterworld.com/forum91/8.htm</a><br> </p> <p><a target="_blank" href="http://chiragrdarji.wordpress.com/2007/05/28/thousand-separator-function-for-java-script/">http://chiragrdarji.wordpress.com/2007/05/28/thousand-separator-function-for-java-script/</a><br> </p> <p><a target="_blank" href="http://lists.evolt.org/pipermail/javascript/2004-June/007396.html">http://lists.evolt.org/pipermail/javascript/2004-June/007396.html</a><br> </p> <p><br> </p> 2010-10-23T21:58:32-04:004138198http://forums.asp.net/p/1616010/4138198.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>Thanks Emad but I want a piece of code that can do this operation on KeyPress event.</p> <p>As you know, the code should remove all commas and then re-add them.</p> <p><br> </p> 2010-10-24T03:54:28-04:004140863http://forums.asp.net/p/1616010/4140863.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>hi Afshin Zavar,</p> <p>then how do you know&nbsp;when you need to re-add the comma on&nbsp;your text when KeyPress event</p> <p>i recommend you&nbsp;implement thousand separator on blur() event</p> <p>please let me know if I misunderstand your requirement, thanks~</p> 2010-10-26T07:34:57-04:004141941http://forums.asp.net/p/1616010/4141941.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>I'm trying to get the content of TextBox... What's wrong with it, please?</p> <p><br> </p> <p><pre class="prettyprint">&lt;head runat=&quot;server&quot;&gt; &lt;title&gt;&lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; //Adds thousand seprator function split(TextBox) { alert(TextBox.text); } //Removes thousand seprator function merge(textbox) { } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;asp:TextBox ID=&quot;txt&quot; runat=&quot;server&quot; onkeypress=&quot;split('&lt;%=txt.clientID%&gt;');&quot; onblur=&quot;merge('&lt;%=txt.clientID%&gt;');&quot;&gt; &lt;/asp:TextBox&gt; &lt;/form&gt; &lt;/body&gt;</pre><br> <br> </p> 2010-10-26T18:52:52-04:004141972http://forums.asp.net/p/1616010/4141972.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>Hi,</p> <p>Follow this :</p> <pre class="prettyprint">&lt;%@ Page Language=&quot;C#&quot; %&gt; &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; &lt;script runat=&quot;server&quot;&gt; &lt;/script&gt; &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; &lt;head id=&quot;Head1&quot; runat=&quot;server&quot;&gt; &lt;title&gt;&lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; //Adds thousand seprator function split(TextBox) { alert(TextBox.value); } //Removes thousand seprator function merge(textbox) { } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;asp:TextBox ID=&quot;txt&quot; runat=&quot;server&quot; onkeyup=&quot;split(this);&quot; onblur=&quot;merge(this);&quot;&gt; &lt;/asp:TextBox&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><br> &nbsp;I changed onkeypress to onkeyup. I think it's more properly</p> 2010-10-26T19:11:12-04:004142453http://forums.asp.net/p/1616010/4142453.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>now, i revised the code and here is the result:</p> <p><pre class="prettyprint">&lt;script type=&quot;text/javascript&quot;&gt; function strrev(str) { if (!str) return ''; var revstr = ''; for (i = str.length - 1; i &gt;= 0; i--) revstr &#43;= str.charAt(i) return revstr; } function ReplaceAll(Source, stringToFind, stringToReplace) { var temp = Source; var index = temp.indexOf(stringToFind); while (index != -1) { temp = temp.replace(stringToFind, stringToReplace); index = temp.indexOf(stringToFind); } return temp; } function AddAndRemoveSeparator(txtbox) { var i = 0, Odd = 0; rev = '', result = ''; txtbox.value = ReplaceAll(txtbox.value, ',', ''); //remove prevoius separators; if (txtbox.value.length &lt;= 3) return; rev = strrev(txtbox.value); //reverse string; while (i &lt; rev.length) { result &#43;= rev.substr(i, 1); Odd&#43;&#43;; if (Odd % 3 == 0) { //add separator after 3 digits; result &#43;= ','; odd = 0; } i&#43;&#43;; } result = strrev(result); // if (result.charAt(1) == ',') { // result = result.substr(2, result.length-1) // } txtbox.value = result; } &lt;/script&gt; &lt;/head&gt; &lt;body onload=&quot;document.form1.TextBox1.focus();&quot;&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Text=&quot;Number:&quot;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; onkeyup=&quot;AddAndRemoveSeparator(this);&quot; Text=&quot;12345&quot;&gt; &lt;/asp:TextBox&gt; &lt;/form&gt;</pre><br> There is a bug that I couldn't fix. sometimes the ',' is at the begining of string and i couldn't get rid of it.</p> <p>Does anybody have any better code or algorithm ?<br> </p> <p><br> </p> 2010-10-27T04:13:29-04:004142637http://forums.asp.net/p/1616010/4142637.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Afshin_Zavar</h4> <br> There is a bug that I couldn't fix. sometimes the ',' is at the begining of string and i couldn't get rid of it.</blockquote> &nbsp; <p></p> <p>Hi,</p> <p>in what state&nbsp; ',' is the begininig ?</p> 2010-10-27T07:19:42-04:004142681http://forums.asp.net/p/1616010/4142681.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>For example when the length of number is 6 (like:123456)<br> </p> 2010-10-27T07:46:19-04:004142708http://forums.asp.net/p/1616010/4142708.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>Hi,</p> <p>test&nbsp;this :</p> <pre class="prettyprint">function strrev(str) { if (!str) return ''; var revstr = ''; for (i = str.length - 1; i &gt;= 0; i--) revstr &#43;= str.charAt(i) return revstr; } function ReplaceAll(Source, stringToFind, stringToReplace) { var temp = Source; var index = temp.indexOf(stringToFind); while (index != -1) { temp = temp.replace(stringToFind, stringToReplace); index = temp.indexOf(stringToFind); } return temp; } function AddAndRemoveSeparator(txtbox) { var i = 0, Odd = 0; rev = '', result = ''; txtbox.value = ReplaceAll(txtbox.value, ',', ''); //remove prevoius separators; if (txtbox.value.length &lt;= 3) return; rev = strrev(txtbox.value); //reverse string; while (i &lt; rev.length) { result &#43;= rev.substr(i, 1); Odd&#43;&#43;; if ((Odd == 3) &amp;&amp; (i != rev.length-1)) { //add separator after 3 digits; result &#43;= ','; Odd = 0; } i&#43;&#43;; } result = strrev(result); // if (result.charAt(1) == ',') { // result = result.substr(2, result.length-1) // } txtbox.value = result; }</pre> <p><br> <br> I change&nbsp;<span class="" style="font-size:x-small; font-family:">(Odd % 3 == 0) in&nbsp;if clause. now when the length of characters is 6 for example&nbsp;(123456) result is 123,456</span></p> 2010-10-27T08:10:42-04:004956368http://forums.asp.net/p/1616010/4956368.aspx/1?Re+Implementation+of+thousand+separator+for+a+TextboxRe: Implementation of thousand separator for a Textbox <p>Thanks very much.This has really been helpful.</p> 2012-04-28T18:05:03-04:00