max character for a text area field

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

here is what i have:

<script language="javascript" type="text/javascript">

    function validate(form){         //var controls = form.elements;

        alert("start");

  if (form.comment[body].length > 256) {             alert('Please limit your message to 256 characters. There are currently ' + form.comment[body].length + '.');             return false;         }         else {             return true;         }     } </script>

<%= error_messages_for :comment %>

<h1>Comments</h1>

<% form_for(@comment, :html => {:onSubmit => 'return validate(this);'}) do |f| %>   <p>     <b>User Name</b><br />     <%= f.text_field(:username, :size => 67) %>   </p>

  <p>     <b>Title</b><br />     <%= f.text_field(:title, :size => 67) %>   </p>

  <p>     <b>Body</b><br />     <%= f.text_area(:body, :size => "50x20") %>   </p>

  <p>     <%= f.submit "Post Coment" %>   </p> <% end %>

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

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

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 Kulik <rails-mailing-l...@andreas-s.net>

I agree u should use validates_length_of rather than a JS for restricting the user to a certain limit

Thanks

Dhaval Parikh Software Engineer Ruby on Rails

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.

<span class='chars_left'>chars left : <span id="blog_counter"><%= UserBlog::MAX_CHARS -                              (@blog.body.length rescue 0) -%></span> </span> <%= text_area_tag :body, (@blog.body rescue nil),      :onkeyup => "$('blog_counter').update([#{UserBlog::MAX_CHARS} -                                             this.value.strip().length,0].max())" %>