1

maxlength attribute in is not working. Is there any alternative ways for this isuue or any suggestion?

Reffrence: https://developer.salesforce.com/docs/component-library/bundle/lightning:input/example#lightningcomponentdemo:exampleInputNumber

1
  • can you should me with your code and some screen shot? Oct 15, 2018 at 6:23

3 Answers 3

3

You could specify a minimum and maximum value. It won't prevent input values outside the range, but will highlight it with a red error message:

<lightning:input label="Some Field" max="9999" min="0" />

Be sure to check validity before saving if you use this technique.

1
  • Thanks for reply, If I enter decimal value it's fire error, so I used step attribute. But I want to enter value in 3.2(Ex.99.25) format for percent field. Do you have any suggetion for percent field? Oct 15, 2018 at 7:25
3

I handle through javascript handler, here is code:

--Component--

<aura:component >
    <lightning:input type="number" aura:id="number" name="input1" 
                     label="Enter a number" maxlength="3" onchange="{!c.CheckLength}"/>
</aura:component>

-- Controller.js----

({
    CheckLength : function(component, event, helper) {
        var val = component.find("number").get('v.value');
        if(val.length > 3){
            var comp = component.find("number");
            comp.set('v.value',val.substring(0,3));
        }
    }
})
0

Its been a while now, but, the lightning:input component now prevents entering additional characters when the set limit has been reached and optionally displays an error message:

<lightning:input maxlength="255" required="true" label="Subject" name="Subject" value="{!v.subjectValue}" messageWhenValueMissing="Complete this field." messageWhenTooShort=" " />

Example (when empty and field is required):

enter image description here

Example (When limit reached): enter image description here

**the char counter is custom, added for visibility purposes.

1
  • It still doesn't work for number type Apr 25, 2019 at 20:35

You must log in to answer this question.

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