Html.TextBoxFor, Currencyhttp://forums.asp.net/t/1790937.aspx/1?Html+TextBoxFor+CurrencyTue, 10 Apr 2012 00:32:21 -040017909374923773http://forums.asp.net/p/1790937/4923773.aspx/1?Html+TextBoxFor+CurrencyHtml.TextBoxFor, Currency <p>I want to be able to display currency in a TextBoxFor. I want the value to display like &#36;1,000.00 I also want, when the user clicks inside the textbox for the &#36; and , to go away and just the numbers are visible for editing. I also want jquery validation to take place against the RegularExpression data annotations in the View Model.</p> <p>Here is my textbox:</p> <pre class="prettyprint">@Html.TextBoxFor(m =&gt; m.Info.Amount, new { Value = String.Format(&quot;{0:C}&quot;, Model.Info.Amount), onblur = &quot;numFormatMoneyWithDecimal(this,'$',',',true)&quot;, onfocus = &quot;RemoveFormat(this)&quot; })</pre> <p>The javascript handles removing the &#36; and , when I enter the textbox.</p> <p>Here is my RegularExpression:</p> <pre class="prettyprint">[RegularExpression(@"^\&#36;?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])&#36;", ErrorMessage = "Amount: only numbers followed by a decimal point followed by two numbers allowed.")] public Nullable&lt;decimal&gt; Amount { get; set; }</pre> <p>But the problem is when I validate the ErrorMessage displays.</p> <p>What is the best way to approach what I want to do? I want the &#36; and , stripped before I save to the database.<br> <br> </p> 2012-04-09T22:09:46-04:004923777http://forums.asp.net/p/1790937/4923777.aspx/1?Re+Html+TextBoxFor+CurrencyRe: Html.TextBoxFor, Currency <p>Look into a client side library to do this for you. There shouldn't be a reason to submit the form with the currency format in the textbox, that's only for display.</p> <p>Look at the KendoUi Numeric box (which is only client side). It shows a formatted currency, but removes formatting when a user edits. The formatting is also NOT send to the controller, only the number<br> <a href="http://demos.kendoui.com/web/numerictextbox/index.html">http://demos.kendoui.com/web/numerictextbox/index.html</a>&nbsp;</p> 2012-04-09T22:17:43-04:004923834http://forums.asp.net/p/1790937/4923834.aspx/1?Re+Html+TextBoxFor+CurrencyRe: Html.TextBoxFor, Currency <p>I am looking for suggestions on how to approach this without purchasing additional libraries.</p> 2012-04-09T23:49:41-04:004923850http://forums.asp.net/p/1790937/4923850.aspx/1?Re+Html+TextBoxFor+CurrencyRe: Html.TextBoxFor, Currency <p></p> <blockquote><span class="icon-blockquote"></span> <h4>NickKA</h4> <p></p> <p>I am looking for suggestions on how to approach this without purchasing additional libraries.</p> <p></p> </blockquote> <p></p> <p>You don't have to purchase anything. KendoUI has an open source version that includes the numeric input box. As long as your company is ok with open source software then you can use it free.&nbsp;</p> <p>In addition, there are other similar libraries that you can use, such at this plugin which is also open source<br> <a href="http://digitalbush.com/projects/masked-input-plugin/">http://digitalbush.com/projects/masked-input-plugin/</a></p> 2012-04-10T00:32:21-04:00