Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

2 Answers 2

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 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 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 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 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 at 19:36

Download static resource from this location

<apex:page sidebar="false" showHeader="false" standardStylesheets="false">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"    type="text/javascript"></script>
<script src="{!URLFOR($Resource.charCounter)}" type="text/javascript"></script>
  <script type="text/javascript">
  $(document).ready(function(){

    $('.countable1').jqEasyCounter({
        'maxChars': 75,
        'maxCharsWarning': 70
    });

    $('.countable2').jqEasyCounter({
        'maxChars': 50,
        'maxCharsWarning': 40,
        'msgFontSize': '12px',
        'msgFontColor': '#000',
        'msgFontFamily': 'Verdana',
        'msgTextAlign': 'left',
        'msgWarningColor': '#F00',
        'msgAppendMethod': 'insertBefore'               
      });

});
</script>
</head>
<apex:form >
 <body>

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


 </body>
 </apex:form>
</apex:page>
share|improve this answer
    
thank you for your answer, I am sure it will help others who are comfortable using javascript. –  ths Apr 17 at 19:44

Your Answer

 
discard

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.