I’m looking for a way to display the number of characters remaining in a field as the user types. I know how to get the length (number of characters already entered), but I don’t know how to calculate the maximum characters allowed in a given field without hard coding that in.
Also, once I have the calculation, is there a way for the function to run as the user types. I know the onDataChange() method will work, but only when the user is finished and then clicks off the field.
If you want to use it in web client, you can try using Jquery keyup function through webClientUtils plugin and return the remaining charecters using callback method.
function getFieldMaxLength(elementName) {
var id = plugins.WebClientUtils.getElementMarkupId(elementName);
var callBack = plugins.WebClientUtils.generateCallbackScript(callbackMeth,['text_remaining', 'text_max']);
var onKeyUpJs = "$('#"+id+"').keyup(function() {"+
"var text_length = $('#"+id+"').val().length;"+
"var text_max = $('#"+id+"').attr('maxlength');"+
"var text_remaining = text_max - text_length;"+
callBack +
"});"
plugins.WebClientUtils.executeClientSideJS(onKeyUpJs);
}
function callbackMeth(remaining, max){
elements.lbl_show_remaining.text = remaining +" charecters out of "+ max +" charecters.";
}
Hope this will help you!
If only for smart client you can go for keyListner plugin. I am not sure if it is web client compatible or not.