You need to sign in to do that
Don't have an account?
matthewGTS
lightning:input doesn't validate negative decimal
I have a form where people can enter a value between zero and one. I am using the following element to capture their input
However, if I instead enter that value as "-.01" (without the zero to the left of the decimal point), the input element does NOT identify it as an invalid value.
I had to write extra code to check the value before the form gets submitted to compensate for this.
Is this a bug, or is there a configuration that I am missing?
<lightning:input name="example" label="example" type="number" min="0.0" max="1.0" step="0.01"/>Negative values, being less than the minimum, should not be valid. Entering "-0.01" is correctly identified as an invalid value.
However, if I instead enter that value as "-.01" (without the zero to the left of the decimal point), the input element does NOT identify it as an invalid value.
I had to write extra code to check the value before the form gets submitted to compensate for this.
Is this a bug, or is there a configuration that I am missing?
For anyone else who stumbles upon this later, this is what I went with in the end: and then in the controller js that runs on submit I have
All Answers
Or use javascript validation
You can use a regex in javascript in the controller with onchange="{!c.myvalidate}"
var regex = /^[^01]/; pattern of error means: not begins with 0 or 1
The value is set to zero if the pattern of error is found.
There is messageWhenBadInput that also exists but the change of value itself with zero is more stable.
https://developer.salesforce.com/docs/component-library/bundle/lightning:input/specification
Below Code can fulfill your requirements, Hope this will work for you.
type 1 :
<lightning:input name="example" label="example" type="number" step="0.01"/>
type 2 :
<lightning:input name="example" label="example" type="number" min="-0.1" max="1.0" step="0.01"/>
Please mark this as best answer if this solves your problem.
Thank you,
Ajay Dubedi
It is better to let the field empty if the value is not correct.
For anyone else who stumbles upon this later, this is what I went with in the end: and then in the controller js that runs on submit I have