maxlength attribute in is not working. Is there any alternative ways for this isuue or any suggestion?
3 Answers
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.
-
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
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));
}
}
})
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):
**the char counter is custom, added for visibility purposes.