import { Context } from "koa"; import { FormControl, SimpleInput } from "../controls/controls.js"; import { FormDataValue } from "../form-types.js"; import { FormField } from "./field.js"; export class PhoneNumberWithoutCountryCode< Required extends boolean, > extends FormField { public getEmptyValue(): string { return "000 000 000"; } async parse(ctx: Context, raw_value: FormDataValue) { if (typeof raw_value == "string") { return { parsable: true, parsed_value: raw_value .replaceAll(/[^0-9]/, "") .replace(/(\d{3})(?=\d)/g, "$1 ") .trim(), error: null, }; } else { return { parsable: false, parsed_value: null, error: "Expected string", }; } } getControl(): FormControl { return new SimpleInput(this, { inputmode: "tel", type: "text", placeholder: "123 456 789", }); } }