Fix possibility to add letters to number input

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2019-12-09 14:13:38 +01:00
parent fc6fcd4569
commit 93adf6d225
No known key found for this signature in database
GPG Key ID: B143FF27EF555D16

View File

@ -1,6 +1,7 @@
import InputIdentity from './Interfaces/InputIdentity'; import InputIdentity from './Interfaces/InputIdentity';
import DefaultInput from './DefaultInput'; import DefaultInput from './DefaultInput';
import { toNumber, isNumber } from '../Functions'; import { toNumber, isNumber } from '../Functions';
import FormManager from '../FormManager';
/** /**
* *
@ -9,13 +10,26 @@ import { toNumber, isNumber } from '../Functions';
*/ */
export default class NumberInput extends DefaultInput { export default class NumberInput extends DefaultInput {
constructor(element: HTMLElement, form: FormManager) {
super(element, form)
const regex = new RegExp("^[0-9.]{1}|Backspace$")
element.addEventListener("keydown", (ev: KeyboardEvent) => {
if (!regex.test(ev.key) && !ev.ctrlKey) {
ev.preventDefault()
}
})
}
public formatValue(value: any): number|undefined { public formatValue(value: any): number|undefined {
const n = toNumber(value) const n = toNumber(value)
return n return n
} }
public verify(): boolean { public verify(): boolean {
return typeof this.getValue() === "undefined" || isNumber(this.getValue()) const val = this.getValue()
if ( val === undefined && this.element.hasAttribute("required")) return false
return typeof val === "undefined" || isNumber(val)
} }
public static identity: InputIdentity = { public static identity: InputIdentity = {