mirror of
https://github.com/dzeiocom/FormManager.git
synced 2025-04-22 10:52:16 +00:00
Fix possibility to add letters to number input
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
fc6fcd4569
commit
93adf6d225
@ -1,6 +1,7 @@
|
||||
import InputIdentity from './Interfaces/InputIdentity';
|
||||
import DefaultInput from './DefaultInput';
|
||||
import { toNumber, isNumber } from '../Functions';
|
||||
import FormManager from '../FormManager';
|
||||
|
||||
/**
|
||||
*
|
||||
@ -9,13 +10,26 @@ import { toNumber, isNumber } from '../Functions';
|
||||
*/
|
||||
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 {
|
||||
const n = toNumber(value)
|
||||
return n
|
||||
}
|
||||
|
||||
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 = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user