We're using the onchange
event on text-type lightning-input
components to apply formatting, so users can quickly enter numbers and have the punctuation applied automatically.
For instance, a Social Security Number entered as "123456789" becomes "123-45-6789" when the 9th digit is entered, via this onchange
function:
handleSSNValueChange(e) {
let input = e.target.value.replace(/\D/g,''); // Remove non-numeric characters
if(input.length >= 9) {
let first = input.substring(0, 3);
let mid = input.substring(3, 5);
let last = input.substring(5, 9);
e.target.value = `${first}-${mid}-${last}`;
}
}
I'd like to do the same with a date-type lightning-input
, so that "01022020" becomes Jan 2, 2020. When I tried, it appeared that that e.target.value
doesn't hold the actual string being entered. It apparently gets set on blur, when the typed date has been parsed. I would need a way to access the string value being displayed on the front end in order to do any kind of custom string interpretation.
Any workarounds?
The date format is automatically validated against the user's Salesforce locale format during the blur event.
So it seems that this matches the user's settings in Salesforce. – zaitsman Jan 28 '20 at 23:48