Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:24:51+00:00 2026-06-14T20:24:51+00:00

I have a comment model where I limit the maximum length of a comment

  • 0

I have a comment model where I limit the maximum length of a comment like:

validates_length_of :comment, :maximum => 500

In my view I have input field declared:

<%= f.text_area :comment,:as => :text, :maxlength => 500 %>

The limit on the input field works as expected, it limits to maximum 500 chars.

However, the model limit does not work as expected. A text of 500 chars with newlines gives a validation error. The model counts newlines as two characters (and possible other characters too). So

This input will work, no newlines:

 abc abc abc abc....

This will not:

abc

abc
.
.

Is there a simple way to make validates_length_of to count newlines (and other) as one character?.

===Result1===

I combined the great answers from Jon and Dario and created this:

before_validation(:on => :create) do
  self.comment = comment.gsub("\r\n","\n") if self.comment
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:24:52+00:00Added an answer on June 14, 2026 at 8:24 pm

    Browsers send newlines from textareas as “\r\n”
    Effectively each newline is counted as two chars when using Rails default length validator

    So either make a replace method in the controller, or make a custom length validator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comment model with string property like this: [Column] public string Text
I have a data model where a User can like a Project, Suggestion, Comment
I have a message form which uses validates_length_of in the model to limit the
I have a comment model that posts under a micropost like facebook. The problem
I have a Comment model in my app but am encountering a lot of
I have Comment as a polymorphic model. It is attached to Post, Review, etc.
I have a content model and a comment model. On insertion of comment I
In my app I have a article and comment model. I implemented a reply-feature,
I have a YML file containing fixtures for a Rails model (Comment) which looks
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.