Minimalist JavaScript to make form input formatting simple
See an example
Format the input element to whatever you like and only submit the raw data.
This custom element is 1.1 kB minified.
Define your formatter:
``js
(() => {
let USDollar = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
window.currency = {
format(value) {
return USDollar.format(value)
},
isValid(value) {
return (isNaN(value))
? 'Must be a number'
: ''
}
}
})()
`
Put on your HTML page:
`js`
See the tests to see how it works!
Improved handling of invalid data.
Added ability to insert error message in element using data-show-invalid`.