forked from MapComplete/MapComplete
Add phone number validation and auto-formatting to the correct international format
This commit is contained in:
parent
1372027dac
commit
887e049f2c
4 changed files with 90 additions and 9 deletions
|
@ -16,7 +16,9 @@ import {RadioButton} from "../UI/Input/RadioButton";
|
|||
import Translations from "../UI/i18n/Translations";
|
||||
import Locale from "../UI/i18n/Locale";
|
||||
import * as EmailValidator from 'email-validator';
|
||||
import { parsePhoneNumberFromString } from 'libphonenumber-js'
|
||||
import {parsePhoneNumberFromString} from 'libphonenumber-js'
|
||||
|
||||
|
||||
export class TagRenderingOptions implements TagDependantUIElementConstructor {
|
||||
|
||||
|
||||
|
@ -27,11 +29,16 @@ export class TagRenderingOptions implements TagDependantUIElementConstructor {
|
|||
"nat": (str) => str.indexOf(".") < 0 && !isNaN(Number(str)) && Number(str) > 0,
|
||||
"float": (str) => !isNaN(Number(str)),
|
||||
"email": (str) => EmailValidator.validate(str),
|
||||
"phone": (str) => parsePhoneNumberFromString(str).isValid()
|
||||
"phone": (str, country) => {
|
||||
return parsePhoneNumberFromString(str, country.toUpperCase())?.isValid() ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
public static formatting = {
|
||||
"phone": (str) => parsePhoneNumberFromString(str).formatInternational()
|
||||
"phone": (str, country) => {
|
||||
console.log("country formatting", country)
|
||||
return parsePhoneNumberFromString(str, country.toUpperCase()).formatInternational()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -352,8 +359,9 @@ class TagRendering extends UIElement implements TagDependantUIElement {
|
|||
|
||||
const prepost = Translations.W(freeform.template).InnerRender().split("$");
|
||||
const type = prepost[1];
|
||||
|
||||
let isValid = TagRenderingOptions.inputValidation[type];
|
||||
if(isValid === undefined){
|
||||
if (isValid === undefined) {
|
||||
console.log("Invalid type for field type", type)
|
||||
isValid = (str) => true;
|
||||
}
|
||||
|
@ -364,10 +372,10 @@ class TagRendering extends UIElement implements TagDependantUIElement {
|
|||
if (string === "" || string === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if (!isValid(string)) {
|
||||
if (!isValid(string, this._source.data._country)) {
|
||||
return undefined;
|
||||
}
|
||||
const tag = new Tag(freeform.key, formatter(string));
|
||||
const tag = new Tag(freeform.key, formatter(string, this._source.data._country));
|
||||
|
||||
if (freeform.extraTags === undefined) {
|
||||
return tag;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue