$form['my_textarea'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#default_value' => $node->body,
'#required' => TRUE,
'#description' => t('The main body text of your entry. Please note that this is limited to %limit characters or less.', array('%limit'=>$max_length)),
// this is the item to be added - limit length while typing
'#attributes' => array('onKeyPress' => "return textareaMaxLength(this, event, $sms_max_len);"),
);
Create a JS file and add it using drupal_add_js:
drupal_add_js(drupal_get_path('module', 'mymodule'). '/myfile.js');
Inside myfile.js define the following function:
function textareaMaxLength(field, evt, limit) {
var evt = (evt) ? evt : event;
var charCode =
(typeof evt.which != "undefined") ? evt.which :
((typeof evt.keyCode != "undefined") ? evt.keyCode : 0);
if (!(charCode >= 13 && charCode <= 126)) {
return true;
}
return (field.value.length < limit);
}
var evt = (evt) ? evt : event;
var charCode =
(typeof evt.which != "undefined") ? evt.which :
((typeof evt.keyCode != "undefined") ? evt.keyCode : 0);
if (!(charCode >= 13 && charCode <= 126)) {
return true;
}
return (field.value.length < limit);
}
Nice explain about drupal form API.
ReplyDeleteDrupal Development Services