<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - how to use Rails NumberHelper methods on client-side?]]></title>
		<link>http://railsforum.com/viewtopic.php?id=38723</link>
		<description><![CDATA[The most recent posts in how to use Rails NumberHelper methods on client-side?.]]></description>
		<lastBuildDate>Wed, 05 May 2010 12:34:35 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: how to use Rails NumberHelper methods on client-side?]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=123881#p123881</link>
			<description><![CDATA[<p>Brad,</p><p>Thanks! Your response marks one of the most complete I&#039;ve seen, much less enjoyed direct use of.</p><p>Thanks again,</p><p>Grar</p>]]></description>
			<author><![CDATA[dummy@example.com (grary)]]></author>
			<pubDate>Wed, 05 May 2010 12:34:35 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=123881#p123881</guid>
		</item>
		<item>
			<title><![CDATA[Re: how to use Rails NumberHelper methods on client-side?]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=123823#p123823</link>
			<description><![CDATA[<p>There is no real straightforward direct way to leverage rails format helpers on the browser/java script side.&nbsp; &nbsp;</p><p>One approach, define formatted versions of your attributes that need indicative defaults, i.e. in your model which contains an attributed named <strong>&#039;field_needing_hint&#039;</strong>,&nbsp; assume you want to indicate a dollar amount be entered:</p><br /><div class="codebox"><pre><code>def cvt_dollar(str)
  str.gsub!(/[^0-9.]/,&#039;&#039;) 
end    
def fmt_dollar(fld) 
  &quot;$ &quot; + fld.to_s.reverse.gsub(/...(?=.)/,&#039;\&amp;,&#039;).reverse
end
def  formatted_field_needing_hint
  fmt_dollar(field_needing_hint) 
end
def formatted_field_needing_hint=(str)
  self.field_needing_hint = cvt_dollar(str)
end</code></pre></div><p>I also use an onblur event handler on the client side to enforce a proper dollar format, i.e.:<br /></p><div class="codebox"><pre><code>formatDollar = function(num) {
  num = num.toString().replace(/\$|\,/g,&#039;&#039;);
  if(isNaN(num))
    num = &quot;0&quot;;
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents&lt;10)
    cents = &quot;0&quot; + cents;
  for (var i = 0; i &lt; Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+&#039;,&#039;+
      num.substring(num.length-(4*i+3));
  return (((sign)?&#039;&#039;:&#039;-&#039;) + &#039;$ &#039; + num );
}</code></pre></div><p>That&#039;s just some random JavaScript formatting code, use whatever you like,&nbsp; then in my views:<br /></p><div class="codebox"><pre><code>form_for @thing  do |f|
    text_field :formatted_field_needing_hint , :onblur=&gt;&quot;this.value=formatDollar(this.value);&quot;
end</code></pre></div><p>So if field_needing_hint is nil or contains &quot;0&quot;,&nbsp; the user will be prompted with &quot;$ 0&quot; as the default value.</p>]]></description>
			<author><![CDATA[dummy@example.com (BradHodges)]]></author>
			<pubDate>Tue, 04 May 2010 17:32:25 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=123823#p123823</guid>
		</item>
		<item>
			<title><![CDATA[how to use Rails NumberHelper methods on client-side?]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=123810#p123810</link>
			<description><![CDATA[<p>Hi,</p><p>I&#039;d like to very strongly hint at the acceptable format of some of my textfields by &#039;prepopulating&#039; them with indicative characters when the user enters the fields. So, with some effort I could devise javascripts that populate textfields with &#039;%&#039; or currency signs or separate digits with commas. Yet, these are all things that Rails&#039; NumberHelper will do on the server-side. Ideally, I could just call those utilities on the client-side. Any suggestions on how to do this or otherwise accomplish my task using existing resources?</p><p>Thanks,</p><p>Grar</p>]]></description>
			<author><![CDATA[dummy@example.com (grary)]]></author>
			<pubDate>Tue, 04 May 2010 13:00:52 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=123810#p123810</guid>
		</item>
	</channel>
</rss>
