0

I am trying to limit my input number field to exactly match the field values which are 16 digits to 2 decimal digits. Also I want to prevent user from putting something different into the field like a string.

I tried several things but didnt work like max length 18 counts dot as 1 char and allow values like 18 digit with no decimals witch wont be saved due to the limits and will throw an error. I also tried regex but some how couldnt solve the problem.

What should I do? Golden rule is to limit value 'exactly' as the field.

Aura

<lightning:input aura:id="Amount" required="true" type="number" name="Amount" step=".01" 
onchange="{!c.validateInput}" value="{!v.newRecord.Amount__c}" label="Amount" />

JS

validateInput: function(component, event, helper) {
    

    var regex = /^\d{0,16}(\.\d{0,2})?$/;
    var inputElement = event.getSource();
    var inputValue = inputElement.get("v.value");
    if (!regex.test(inputValue)) {
        for (let i = 0; i < inputValue.length; i++) {
            var regtest = regex.test(inputValue[i]);
            console.log(regtest);
            if(!regtest){
                inputValue = inputValue.replace(inputValue[i], "");
                break;
            }
          }
          console.log(inputValue);

          component.set("v.newPaymentInformRecord.Amount__c", inputValue);
    }
}

1 Answer 1

1

Try adding the following to your <lightning:input>:

  • step="0.01"
  • max="9999999999999999" (maxlength is just for the length of the string, as you saw, whereas max is for numbers)
1
  • Sometimes things do not need to be that complicated. Thx fred.
    – YCS
    Commented Oct 23, 2023 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .