Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Is it possible to calculate two or more number entered in the same field ?

Avatar

Level 1

What 'm trying to do is to caculate a book pages number for a copying them.

For example I want to copy pages from 25 to 30 , so the user will write 25-30

the number of copies will equal sum = "30 - 25 + 1"

and if the user write only a single number, the number of copies will equal "sum = sum + 1"

So, the user input will be like that "25-30,45-60,65,67"

I'm thinking of using an Array and a "," to split

var sumArray = new Array();

sumArray = sum.split(",");

Are there any possible way to do this ??

Thanx

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi, I think you could use a RegEx but I would use a split as well, something like;

var pageNumbers = TextField1.rawValue.split(",");

var sum = 0;
var isValid = true;
for (var i = 0; i < pageNumbers.length; i++)
{
var pages = pageNumbers[i].split("-");
// Check for all number elements
if (!pages.every(function(element) { return (element == parseInt(element, 10)); } ) )
{
  xfa.host.messageBox("Please enter comma separated list of pages number, with a range separated by a hyphen, e.g. 25-30,45-60,65,67");
  isValid = false;
  break;
}
if (pages.length > 2)
{
  xfa.host.messageBox("Please enter a page range separated by a hyphen, e.g. \"45-60\"");
  isValid = false;
  break; 
}
if (pages.length == 1)
{
  sum++;
}
else
{
  var start = parseInt(pages[0]);
  var end = parseInt(pages[1]);
  if (end < start)
  {
   xfa.host.messageBox("Please enter a valid page range separated by a hyphen, with the start page first, e.g. \"45-60\"");
   isValid = false;
   break; 
  }
  sum += (end - start + 1);
}
}
if (isValid)
{
app.alert(sum);
}

Might need some more error handling but hopefully will get you started.

Regards

Bruce

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Hi, I think you could use a RegEx but I would use a split as well, something like;

var pageNumbers = TextField1.rawValue.split(",");

var sum = 0;
var isValid = true;
for (var i = 0; i < pageNumbers.length; i++)
{
var pages = pageNumbers[i].split("-");
// Check for all number elements
if (!pages.every(function(element) { return (element == parseInt(element, 10)); } ) )
{
  xfa.host.messageBox("Please enter comma separated list of pages number, with a range separated by a hyphen, e.g. 25-30,45-60,65,67");
  isValid = false;
  break;
}
if (pages.length > 2)
{
  xfa.host.messageBox("Please enter a page range separated by a hyphen, e.g. \"45-60\"");
  isValid = false;
  break; 
}
if (pages.length == 1)
{
  sum++;
}
else
{
  var start = parseInt(pages[0]);
  var end = parseInt(pages[1]);
  if (end < start)
  {
   xfa.host.messageBox("Please enter a valid page range separated by a hyphen, with the start page first, e.g. \"45-60\"");
   isValid = false;
   break; 
  }
  sum += (end - start + 1);
}
}
if (isValid)
{
app.alert(sum);
}

Might need some more error handling but hopefully will get you started.

Regards

Bruce