These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Input validation - no decimals

cyfu
Registered: Apr 26 2011
Posts: 32
Answered

I'm trying to use the following code asking a user to enter a number and if it has a decimal then an error message.
 
vNumSamples = Number(validateNumSamples);
isWhole_re = Number(/^\s*\d+\s*$/);
if (isNaN(vNumSamples)) {
app.alert("Invalid \"Number of Samples\" value. It must be a number.");
return false;
}

if (vNumSamples<=0 || vNumSamples>12) {
app.alert("Invalid \"Number of Samples\" value. It must be between 1 and 12.");
return false;
}

if (isWhole_re != -1) {
app.alert("Invalid \"Number of Samples\" value. It must be a whole number (digits only)");
return false;
}
 
The problem I'm getting is that any number entered results in the error message:
Invalid Number of Samples value. It must be a whole number (digits only)
 
Is isWhole_re = Number(/^\s*\d+\s*$/); the correct code to check for whole numbers only?

My Product Information:
Acrobat Pro 7.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
No, that's not correct. Consider a different approach by limiting the entry to just digits by using a custom Keystroke script. If you add the following function in a document-level JavaScript and call it from the Keystroke event, the user will only be able to enter digits. If you set the character limit of the field to 2, it won't accept anything larger than 99 and smaller than 0. Note that you have to include the function in a document-level script in order to avoid weirdness when you use certain internal functions provided by Acrobat (AFMergeChange, AFExactMatch) in your own field-level scripts.

  1. // Document-level function
  2. function DigOnlyKS() {
  3.  
  4. // Get all that is currently in the field
  5. var val = AFMergeChange(event);
  6.  
  7. // Reject entry if anything but digits
  8. event.rc = AFExactMatch(/\d*/, val);
  9. }
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Call this function using the following custom Keystroke script:

DigOnlyKS();

You can then simplify your validation code since all you have to do is check if there's a value, and if so, if it's within the specified range.
cyfu
Registered: Apr 26 2011
Posts: 32
George,
thanks for the response, but I forgot to mention that this input validation is happening within a Dialog. In the dialog, it's asking the user to enter the Number of Samples.

validateNumSamples = results["str3"]; // str3 is item_id holding the # of samples.
I tried adding the following from your code:

vNumSamples = Number(validateNumSamples);
var valNumSamples = AFMergeChange(event);
if (isNaN(vNumSamples)) {
app.alert("Invalid \"Number of Samples\" value. It must be a number.");
return false;
}
if (vNumSamples<=0 || vNumSamples>12) {
app.alert("Invalid \"Number of Samples\" value. It must be between 1 and 12.");
return false;
}
if (event.rc = AFExactMatch(/\d*/, valNumSamples)) {
app.alert("Invalid \"Number of Samples\" value. It must be a whole number (digits only)");
return false;
}

It's not catching a inputted decimal value.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
I see, I didn't understand what you were doing at first. The code I posted isn't appropriate for this case, but try something like the following:

vNumSamples = Number(validateNumSamples);

if (isNaN(vNumSamples)) {
app.alert("Invalid \"Number of Samples\" value. It must be a number.");
return false;
}

if (AFExactMatch(/\d*/, validateNumSamples)) {
app.alert("Invalid \"Number of Samples\" value. It must be a positive whole number (digits only)");
return false;
}

if (vNumSamples<1 || vNumSamples>12) {
app.alert("Invalid \"Number of Samples\" value. It must be from 1 to 12");
return false;
}

cyfu
Registered: Apr 26 2011
Posts: 32
Using the new code I'm getting the same problem as the original post. Any number entered is resulting in the error message:
Invalid Number of Samples value. It must be a whole number (digits only)

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
Sorry, I left off an important part. Change the one line to:

if (!AFExactMatch(/\d*/, validateNumSamples)) {


Note the exclamation mark. It may be better to use:

if (!/^\d*$/.test(validateNumSamples)) {