# Display characters remaining in a field

**URL:** <https://forum.servoy.com/t/display-characters-remaining-in-a-field/18502>\
**Category:** Classic Servoy\
**Created:** [July 8, 2015, 1:51pm UTC](https://forum.servoy.com/t/display-characters-remaining-in-a-field/18502 "2015-07-08T13:51:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wes.s.grayson](https://avatars.discourse-cdn.com/v4/letter/w/f19dbf/32.png) [@wes.s.grayson](https://forum.servoy.com/u/wes.s.grayson)\
**Post date:** [July 8, 2015, 1:51pm UTC](https://forum.servoy.com/t/display-characters-remaining-in-a-field/18502/1 "2015-07-08T13:51:46Z")

</div>

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.

thanks!  
Wes

---

<div class="post-metadata">

**Author:** ![Peter\_de\_Groot](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@Peter\_de\_Groot](https://forum.servoy.com/u/Peter_de_Groot)\
**Post date:** [July 9, 2015, 5:25am UTC](https://forum.servoy.com/t/display-characters-remaining-in-a-field/18502/2 "2015-07-09T05:25:30Z")

</div>

See the keylisteners plugin at [Overview - KeyListeners Plugin - ServoyForge](https://www.servoyforge.net/projects/keylisteners)

Regards,

Peter

---

<div class="post-metadata">

**Author:** ![sovanm](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@sovanm](https://forum.servoy.com/u/sovanm)\
**Post date:** [July 9, 2015, 11:53am UTC](https://forum.servoy.com/t/display-characters-remaining-in-a-field/18502/3 "2015-07-09T11:53:23Z")

</div>

Hello Wes,

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.

```auto
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.

Thanks

---

<div class="post-metadata">

**Author:** ![wes.s.grayson](https://avatars.discourse-cdn.com/v4/letter/w/f19dbf/32.png) [@wes.s.grayson](https://forum.servoy.com/u/wes.s.grayson)\
**Post date:** [July 10, 2015, 6:02pm UTC](https://forum.servoy.com/t/display-characters-remaining-in-a-field/18502/4 "2015-07-10T18:02:35Z")

</div>

Thank you for the reply. I’m using Smart Client so I’ll give the plugin a try. Sovanm, thank you for the sample solution.
