function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
uma52551.3972270309784705E12uma52551.3972270309784705E12 

<apex:inputField value={!strBillingStreet...}

Hi All,

I am kind of stuck with this inputField value attribute, can any one help me in finding this?

Actually I want to Limit the length of "Street" field to 30 characters. By searching online they suggested me to write Javascript function and write apex inputField instead of inputText but giving me error that value field is not an Sobject field.
Can any one please help me how to limit the text field to 30 characters? thank you so much in advance
Best Answer chosen by uma52551.3972270309784705E12
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi,
Here is the simple example no need to write any java script for this..

<apex:inputText size="40" maxLength="30"  label="Physical Street"  styleClass="form" id="PhysicalStreet" value="{!strBillingStreet}"/?

All Answers

NehalNehal (Salesforce Developers) 
Hi,

Yes you can achieve this through the JavaScript.
For that you have to write a JavaScript method which will discard all the character after the maxlength.
Another way is that if you can use  the <apex:inputtext>  instead of <apex:inputfield> here you have the option of Maxlength so you can directly put the integer value here and it will not accept value more than that.

Also in case you want to display the number of characters left  as a counter try the following:-
<form name=""myForm""
action=""/articles/articles/javascript/dynamictextareacounter.asp?ID=<%=siteID%>""
method=""post"">
<b>One Function to Count and Limit Multiple Form Text Areas</b><br>
<textarea name=""message1"" wrap=""physical"" cols=""28"" rows=""5""
onKeyDown=""textCounter(document.myForm.message1,document.myForm.remLen1,125)""
onKeyUp=""textCounter(document.myForm.message1,document.myForm.remLen1,125)""></textarea>
<br>
<input readonly type=""text"" name=""remLen1"" size=""3"" maxlength=""3"" value=""125"">
characters left
<br>
<textarea name=""message2"" wrap=""physical"" cols=""28"" rows=""5""
onKeyDown=""textCounter(document.myForm.message2,document.myForm.remLen2,125)""
onKeyUp=""textCounter(document.myForm.message2,document.myForm.remLen2,125)""></textarea>
<br>
<input readonly type=""text"" name=""remLen2"" size=""3"" maxlength=""3"" value=""125"">
characters left
<br>
<input type=""Submit"" name=""Submit"" value=""Submit"">
<br>
</form>
Here is the javascript function:?-
<SCRIPT LANGUAGE=""JavaScript"">
<!-- Dynamic Version by: Nannette Thacker -->
<!-- http://www.shiningstar.net -->
<!-- Original by :  Ronnie T. Moore -->
<!-- Web Site:  The JavaScript Source -->
<!-- Use one function for multiple text areas on a page -->
<!-- Limit the number of characters per textarea -->
<!-- Begin
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}
//  End -->
</script>

I hope this helps. Please mark this as a ""Best Answer"" if this has resolved your issue.
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi Nehal,


Actually my problem is with Apex:inputField not with textArea and more over i am working on visualforce page there it is not allowing me to give name to the form as well. Please help me how to limit <apex:inputField> to 30 characters. I am pasting my code here.

<td><apex:inputField Id="PhyscialStreet" value="{!strBillingStreet}" onkeydown="limitFieldValue('{!$Component.PhyscialStreet}');" onkeyup="limitFieldValue('{!$Component.PhyscialStreet}');"/></td>


<script LANGUAGE="JavaScript">
    function limitFieldValue(field){
            document.write(field);
            if(document.getElementById(field).value.length >30)}
            document.write(field);
            document.getElementById(field).value  = document.getElementById(field).value.substring(0,30);
            document.write(field);
        }
    }
    </script>
Error: ="{!strBillingStreet}" is not an SObject field.
Sure@DreamSure@Dream
Hi,

Use apex:inputText instead of apex:inputfield. apex:inputfield cannot be used with controller variables


Thanks
Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)
Hi Uma,

<apex:inputField value=""/>
it takes the SObject's field as the parameter. this is the standard features of the salesforce.
So, in your code, instead of apex:inputField, use inputtext:
Follow the following syntax:

<apex:page >

  <script type="text/javascript">
    function limitFieldValue(field){
          if(document.getElementById(field).value.length >3){
                document.write(field);
                document.getElementById(field).value  = document.getElementById(field).value.substring(0,30);
                document.write(field);
          
            }
           
    }
  </script>
  <apex:form >
  <apex:inputText id="PhyscialStreet" onKeyup="limitFieldValue('{!$Component.PhyscialStreet}');"/>
  <!-- <td><apex:inputField Id="PhyscialStreet" value="{!strBillingStreet}" onkeydown="limitFieldValue('{!$Component.PhyscialStreet}');" onkeyup="limitFieldValue('{!$Component.PhyscialStreet}');"/></td>-->
</apex:form>
</apex:page>

Javascrip is case-sensetive language, please consider about the sysntax.

Please try above code and let me know if you find any issues.

Thanks & Regards,
Ranjeet Singh.
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi,
Here is the simple example no need to write any java script for this..

<apex:inputText size="40" maxLength="30"  label="Physical Street"  styleClass="form" id="PhysicalStreet" value="{!strBillingStreet}"/?

This was selected as the best answer
uma52551.3972270309784705E12uma52551.3972270309784705E12
Thank you very much for your help ranjeeth. My problem was solved.