Leading Zeroes in Validation Script of a Specific Length
First off, I am a complete beginner at Javascript for Acrobat, but I've been tasked with moving a form into Acrobat from a very old program. Our department needs the field to be formatted with specifically 3 digits, all being numbers, and with leading zeroes. Anything 1 or 2 digits long has to be prepended with zeroes, and anything over 3 digits needs to be rejected. I have adapted two different validation scripts that work for one part or the other, but not both.
Validation Script 1: This one allows only 3 digits, but does not prepend leading zeroes
event.rc = (event.value=="" || /^\d{3}$/.test(event.value));
I've also tried the keystroke script below in conjunction with the validation script above, but it did not seem to add the zeroes as I'd hoped.
if(!event.willCommit)
{// Reject non-numeric input
event.rc = !/[^\d\.\-]/.test(event.change);
}
else
{// Format with leading zeros
event.value = util.printf("%03d",event.value);
}
Validation Script 2: This one prepends leading zeroes, but will accept more than 3 digits
(function(){
var max_len = 3;
var val = event.value;
while (val.length < max_len) {val = "0" + val;}
event.value = val; })();
My apologies on formatting, I'm new at this and I don't know what the correct format of the script should look like if it's not proper. What do I need to do to make the field accept only 3 digits, and prepend zeroes to any number submitted with less than 3 digits?
Thanks for the help!
