Help limiting an Adobe form field to allow only certain numbers to be entered

New Here ,
Apr 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

Let me start by saying I do not know any coding at all. I'm new to Adobe Pro and I'm trying to create an Adobe form that will be used as a timesheet. The form will have multiple fields where the user needs to enter total hours worked in a day (one field for straight time, another for overtime, another for comp time, etc.). I want to limit the fields to only accept numbers between 0 and 24 in increments of .25. So for example, 8 or 8.25 or 1.5 or 2.75 are all acceptable.

 

If an invalid number is entered, I would like an error message to pop up saying to enter time in increments of .25 and the field to reset to blank. I found code on the internet that worked beautifully but would require me to code every quarter increment between 0 and 24 (including variations such as 1.5 and 1.50 and 2 and 2.0). And the one form has 77 fields (bi-weekly timesheet tracking straight time, OT, Comp Time, vacation, sick, etc.) - I would imaging having 77 fields with that extra long code would just cause a crash or a freeze.

 

I could live without the error message if the field could just reset back to 0 and not allow the invalid entry. Can someong please point me in the right direction? 

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

44

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Most Valuable Participant , Apr 10, 2020 Apr 10, 2020
If you do prefer to use a text field, you can use this code as your custom validation script:   if (event.value) { var v = Number(event.value); event.rc = (v>=0 && v<=24 && v%0.25==0); if (!event.rc) app.alert("You must enter a number between 0 and 24, in multiples of 0.25."); }  

Likes

translate

Translate

Translate
Adobe Community Professional ,
Apr 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

Are you interested in learning how to code?  If you want these sepcific increments, then why not use a dropdown that only has these as the selection. Use two dropdowns to keep it to short dropdown lists, one for the hour and one for the minutes.

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Apr 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

Why not use a drop-down field, then? You can set it to only contain valid values, so the user can't make a mistake and you don't need to mess around with scripts...

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Apr 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

If you do prefer to use a text field, you can use this code as your custom validation script:

 

if (event.value) {
	var v = Number(event.value);
	event.rc = (v>=0 && v<=24 && v%0.25==0);
	if (!event.rc) app.alert("You must enter a number between 0 and 24, in multiples of 0.25.");
}

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

Thank you! Your solution worked perfectly! I prefer to use a text box as opposed to drop-down boxes because the form has 77 fields where a user could enter time and feedback from a sampling of users said they preferred to tab through each field and type in a number as opposed to using a drop-down. Again, thank you!

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines