# Max character for a text area field

**URL:** <https://www.ruby-forum.com/t/max-character-for-a-text-area-field/141265>\
**Category:** Rails\
**Created:** [June 24, 2008, 5:42am UTC](https://www.ruby-forum.com/t/max-character-for-a-text-area-field/141265 "2008-06-24T05:42:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 24, 2008, 5:42am UTC](https://www.ruby-forum.com/t/max-character-for-a-text-area-field/141265/1 "2008-06-24T05:42:06Z")

</div>

i hardly ever write javascript and having a problem validating the max  
characters in a text area field.

here is what i have:

\<%= error\_messages\_for :comment %\>

# Comments

\<% form\_for(@comment, :html =\> {:onSubmit =\> ‘return validate(this);’})  
do |f| %\>

**User Name**  
 \<%= f.text\_field(:username, :size =\> 67) %\>

**Title**  
 \<%= f.text\_field(:title, :size =\> 67) %\>

**Body**  
 \<%= f.text\_area(:body, :size =\> "50x20") %\>

\<%= f.submit "Post Coment" %\>

\<% end %\>

i’m having problems referencing my text area. can someone help?  
thanks!

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 24, 2008, 11:10am UTC](https://www.ruby-forum.com/t/max-character-for-a-text-area-field/141265/2 "2008-06-24T11:10:59Z")

</div>

cant help with the referencing problem, but you should never do a  
(solely)client-side validation of user data anyway.

It’s always best to do this at the server since even though you  
validated it with javascript the user can send you a manipulated  
request with whatever data he chose. So client-side validation would  
only be a user-friendly feature, cause he gets instant feedback.

Rails offers lots of options for (server-side) data validation  
(validates\_presence\_of, validates\_length\_of,  
validates\_numericality\_of…)

hope that helps! good luck, simon

On Jun 24, 5:42 am, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 24, 2008, 11:44am UTC](https://www.ruby-forum.com/t/max-character-for-a-text-area-field/141265/3 "2008-06-24T11:44:58Z")

</div>

smn wrote:

> cant help with the referencing problem, but you should never do a  
> (solely)client-side validation of user data anyway.
> 
> It’s always best to do this at the server since even though you  
> validated it with javascript the user can send you a manipulated  
> request with whatever data he chose. So client-side validation would  
> only be a user-friendly feature, cause he gets instant feedback.
> 
> Rails offers lots of options for (server-side) data validation  
> (validates\_presence\_of, validates\_length\_of,  
> validates\_numericality\_of…)
> 
> hope that helps! good luck, simon
> 
> On Jun 24, 5:42ï¿½am, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I agree u should use validates\_length\_of rather than a JS for  
restricting the user to a certain limit

Thanks

Dhaval P.  
Software Engineer  
Ruby on Rails

> **[RailsHouse : Ruby on Rails Development & Consulting, Waterloo, Canada](https://railshouse.com)**
>
> RailsHouse is Waterloo, Ontario based Ruby on Rails Consultancy firm which provides RoR Development, Migration, Staffing, Maintenace and Support Services

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 25, 2008, 5:20am UTC](https://www.ruby-forum.com/t/max-character-for-a-text-area-field/141265/4 "2008-06-25T05:20:15Z")

</div>

thanks for the help! i used validates\_length\_of and it’s working  
perfectly. Maybe one day i’ll go back to the javascript just to add  
another user friendly reminder so they are aware before they submit the  
form.

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 25, 2008, 5:36am UTC](https://www.ruby-forum.com/t/max-character-for-a-text-area-field/141265/5 "2008-06-25T05:36:26Z")

</div>

On Jun 24, 2008, at 8:20 PM, Scott K. wrote:

> thanks for the help! i used validates\_length\_of and it’s working  
> perfectly. Maybe one day i’ll go back to the javascript just to add  
> another user friendly reminder so they are aware before they submit  
> the  
> form.

Coming in late, but this has worked for me… (wrapped to fit. i  
hope). define the constants as necessary, change the instance  
variables, etc.

chars left :  
 \<%= UserBlog::MAX\_CHARS -  
(@blog.body.length rescue 0) -%\>  
   
\<%= text\_area\_tag :body, (@blog.body rescue nil),  
:onkeyup =\> “$(‘blog\_counter’).update([#{UserBlog::MAX\_CHARS} -  
this.value.strip().length,  
0].max())” %\>
