Changing max allowable length of Text field using Jquery

By | March 11, 2013

In my earlier blog, I have explained how to restrict a length of Sage CRM text field on screen block using JavaScript.
http://www.greytrix.com/blogs/sagecrm/2013/01/14/set-field-length-at-run-time-in-sage-crm/
Here is how you can achieve the same using JQuery. Here explaining the example with Company Name field on Company screen.
1) I have input text field on CRM screen with field name “comp_name” and I want this field allow maximum 10 characters in it.
2) Open Screen customization through Administration [Entity name] | Screens | [Screen name].
3) Paste below jQuery code in Custom Content area.
<Script>
jQuery(document).ready(function()
{
//Assign Max Input length..
var iInputWidthText = 10;
var iInputWidthTextarea = 10;
//’Create Id for all the textarea fields in the screen..
$(‘textarea’).each(function()
{
var sFldName = $(this).attr(“comp_name“)
$(this).attr(“id”,sFldName);
});
 //’Text input Restriction for Input length..
$(‘#comp_name’).keypress(function()
{
if($(this).val().length >= iInputWidthText) {
$(this).val($(this).val().slice(0, iInputWidthText));
return false;
}
});
                //Teatarea Restriction for Input length..
$(‘textarea#comp_note’).keypress(function()
{
if($(this).val().length > iInputWidthTextarea) {
$(this).val($(this).val().slice(0, iInputWidthTextarea));
return false;
}
});
});
</script>
4. Click on save button and check comp_name field will not allow more than 10 characters in it.
5. The above code will work for multiline text box also (i.e. Textarea). You just need to change ‘textarea#comp_note’ with field name.