title: Rails, validate maxlength of input field
body: <p>I have a comment model where I limit the maximum length of a comment like:</p>
  
  <pre><code>validates_length_of :comment, :maximum =&gt; 500
  </code></pre>
  
  <p>In my view I have input field declared:</p>
  
  <pre><code>&lt;%= f.text_area :comment,:as =&gt; :text, :maxlength =&gt; 500 %&gt;
  </code></pre>
  
  <p>The limit on the input field works as expected, it limits to maximum 500 chars.</p>
  
  <p>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</p>
  
  <p>This input will work, no newlines:</p>
  
  <pre><code> abc abc abc abc....
  </code></pre>
  
  <p>This will not:</p>
  
  <pre><code>abc
  
  abc
  .
  .
  </code></pre>
  
  <p>Is there a simple way to make validates_length_of to count newlines (and other) as one character?.</p>
  
  <p>===Result1===</p>
  
  <p>I combined the great answers from Jon and Dario and created this:</p>
  
  <pre><code>before_validation(:on =&gt; :create) do
    self.comment = comment.gsub("\r\n","\n") if self.comment
  end
  </code></pre>