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!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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 http://www.railshouse.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 24, 2008, at 8:20 PM, Scott Kulik 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. <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())" %> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---