Numeric entry, fixed width, zero-pad left, fill from right.

More
9 months 2 weeks ago - 9 months 2 weeks ago #164511 by jlwood
jlwood created the topic: Numeric entry, fixed width, zero-pad left, fill from right.
I have a situation in a survey where users are entering codes in a Multiple Short Text Field. Some of the fields need to be formatted so that the values are a fixed width (5 digits), left padded with zeros when necessary, others can except any word or brief phrase, or be left blank.
I can do this with jquery in the onchange event AFTER the numeric part is entered, but it would be easier and less error prone if users could see the values correctly as they enter them, so for example if the code is "1234" the user would see this as they type:
00000
00001
00012
00123
01234
filling the digits in from the right like a calculator, but keeping the width fixed with zero-padding on the left.
Last Edit: 9 months 2 weeks ago by jlwood. Reason: small typo

Please Log in or Create an account to join the conversation.

More
9 months 2 weeks ago - 9 months 2 weeks ago #164516 by tpartner
tpartner replied the topic: Numeric entry, fixed width, zero-pad left, fill from right.
Place a mask on the input with Robin Herbots Inputmask plugin - github.com/RobinHerbots/Inputmask

I think, for your requirements, the mask script placed in the question source would be something like:

<script type="text/javascript" charset="utf-8">
	$(document).ready(function($) {	 
		$('#question{QID} input[type="text"]').inputmask(
			"9999",
			{ 
				placeholder:"0", 
				numericInput: true
			}
		);
	});
</script>


Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last Edit: 9 months 2 weeks ago by tpartner.

Please Log in or Create an account to join the conversation.

More
9 months 2 weeks ago #164652 by jlwood
jlwood replied the topic: Numeric entry, fixed width, zero-pad left, fill from right.
Thank you Tony, this is working like a charm, and fixing a few other challenges to boot. I'd tried a couple of other simpler masking utilities without success but this did the trick.

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!