Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

the input for my inputtextbox is stored in a value which is a textarea(255). Can I somehow truncate the input field so they cannot put in more then 255 characters? right now it lets them but it causes an error when saving. How do i go about doing this?

share|improve this question
up vote 4 down vote accepted

You can do this is a couple of different ways.

  • Client-side validation using Javascript - There are a number of JS libraries out there for validating form input, including limiting the number of allowed characters in a form field. I think the ideal UX here is a warning letting you know how many characters you have left when you get say, over 200 characters. Check out Validate.js and jQuery Validate for starters.
  • Server-side validation - since this happens out-of-the-box with bound sObject fields, you will get an error kicked back to a Visualforce page when attempting to input more characters into a field than is allowed.

EDIT: If you don't like the standard server-side validation, which takes you away from the form, you can always evaluate form field entry and throw an error back to the page which places the error next to the field in question, just like the way marking a field required does on trying to submit the form without a value in that field. Inside your form submit method, use this code:

if (YOUR_SOBJECT__c.YOUR_TEXT_FIELD__c.length() > 255) {
    YOUR_SOBJECT__c.YOUR_TEXT_FIELD__c.addError('Please enter no more than 255 characters.');
share|improve this answer
Is there a validation rule or such which I can add so it shows an error message when text goes beyond 255. I have no experience with JS so I can't really use it. – ths Apr 17 '14 at 18:29
There is nothing you can do without Javascript that would allow you to validate and provide the user with feedback prior to clicking the submit button on the form. – greenstork Apr 17 '14 at 18:48
When they click save, it shows a large error message basically saying the string is too long. So if I could even get an error message or validate after they click save under the input field it would be totally fine. – ths Apr 17 '14 at 18:50
I get what you're saying now, let me amend my answer to provide the exact way to do this next to the field with server-side validation. – greenstork Apr 17 '14 at 18:56
Hey Thank you very much for your help. The javascript stuff was over my head so I appreciate the simpler solution. – ths Apr 17 '14 at 19:36

Download static resource from this location

<apex:page sidebar="false" showHeader="false" standardStylesheets="false">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src=""    type="text/javascript"></script>
<script src="{!URLFOR($Resource.charCounter)}" type="text/javascript"></script>
  <script type="text/javascript">

        'maxChars': 75,
        'maxCharsWarning': 70

        'maxChars': 50,
        'maxCharsWarning': 40,
        'msgFontSize': '12px',
        'msgFontColor': '#000',
        'msgFontFamily': 'Verdana',
        'msgTextAlign': 'left',
        'msgWarningColor': '#F00',
        'msgAppendMethod': 'insertBefore'               

<apex:form >

 <apex:inputtextarea styleclass="countable1"/>
    <br/> <br/>
 <apex:inputtextarea styleclass="countable2"/>

share|improve this answer
thank you for your answer, I am sure it will help others who are comfortable using javascript. – ths Apr 17 '14 at 19:44
iam using this, and notice it skip count on first character?do you face the same issue? – unidha Dec 28 '15 at 2:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.