mapcomplete/UI/InputElement/Validators/EmailValidator.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
1 KiB
TypeScript
Raw Normal View History

import {Translation} from "../../i18n/Translation.js"
2023-03-29 17:21:20 +02:00
import Translations from "../../i18n/Translations.js"
import * as emailValidatorLibrary from "email-validator"
import {Validator} from "../Validator"
2023-03-29 17:21:20 +02:00
export default class EmailValidator extends Validator {
constructor() {
super("email", "An email adress", "email")
}
isValid = (str) => {
if (str === undefined) {
return false
}
str = str.trim()
if (str.startsWith("mailto:")) {
str = str.substring("mailto:".length)
}
return emailValidatorLibrary.validate(str)
}
reformat = (str) => {
if (str === undefined) {
return undefined
}
str = str.trim()
if (str.startsWith("mailto:")) {
str = str.substring("mailto:".length)
}
return str
}
getFeedback(s: string): Translation {
if (s.indexOf("@") < 0) {
return Translations.t.validation.email.noAt
}
return super.getFeedback(s)
}
}