0

I am am trying to prevent a user from going from a specific number value parameter from say 1 to 9999.99. I have multiple input values that I have assigned ids. I am using a switch statement to set the parameters (min & max) of these, so first would be from 1 to 9999.99, the second be 1 to 999.99, the third 1 to 99.99. However, I can still enter over these values and don't get dinged on my restrictions could some one please provide insight, still trying to work this through. Thanks for any help

function getMinMax(ctrlId) {
  let min = 0;
  let max = 9999;
  switch (ctrlId) {
    case "aID":
      min = 1;
      max = 99999.99;
      break;
    case "bID":
      min = 1;
      max = 999.99;
      break;
    case "cID":
      min = 1;
      max = 99.99;
      break;
       }
  return { min: min, max: max };
}

async function checkMinMax(ctrl) {
  let value = await ctrl.value();
  if(value === '') return;
  let ctrlId = await ctrl.id;
  let minMax = getMinMaxForField(ctrlId);
  let isNotANumber = Number.isNaN(value);
  if(!isNotANumber && Number.parseFloat(value) >= minMax.min && Number.parseFloat(value) <= minMax.max) {
    ctrl.value(value);
  } else {
    alert('Please enter a value between  ' + minMax.min + ' and ' + minMax.max + '.')
    ctrl.value('');
  } 
} 
Sven
  • 1
  • 1
  • Does this answer your question? [How do I set maximum number input from database in javascript?](https://stackoverflow.com/questions/53619582/how-do-i-set-maximum-number-input-from-database-in-javascript) – Daniel Brose May 07 '20 at 00:02
  • the use of async await seems unnecessary ... an elements `id` is not a Promise,, and perhaps you're getting errors (look in your browsers developer tools console) when trying to use the elements value as if it were a function ... it isn't, and you also don't need to await that ... there is nothing asynchronous happening in checkMinmax, so don't use async/await - and finally you assign the value using assignment, since value is not a function – Jaromanda X May 07 '20 at 00:06

0 Answers0