import {Pattern} from "@lion/form-core"; export class IsEmail extends Pattern { /** * Regexes for validating email addresses incl. email in angle-brackets eg. * + "Ralf Becker " * + "Ralf Becker (EGroupware GmbH) " * + "" or "rb@egroupware.org" * + '"Becker, Ralf" ' * + "'Becker, Ralf' " * but NOT: * - "Becker, Ralf " (contains comma outside " or ' enclosed block) * - "Becker < Ralf " (contains < ----------- " ---------------) * * About umlaut or IDN domains: we currently only allow German umlauts in domain part! * We forbid all non-ascii chars in local part, as Horde does not yet support SMTPUTF8 extension (rfc6531) * and we get a "SMTP server does not support internationalized header data" error otherwise. * * Using \042 instead of " to NOT stall minifyer! * * Similar, but not identical, preg is in Etemplate\Widget\Url PHP class! * We can not use "(?@,;:\042\[\]\x80-\xff]+@([a-z0-9ÄÖÜäöüß](|[a-z0-9ÄÖÜäöüß_-]*[a-z0-9ÄÖÜäöüß])\.)+[a-z]{2,}>?$/i; /** * Allow everything containing at least one placeholder e.g.: * - "{{email}}" * - "{{n_fn}} <{{email}}" * - "{{#}}" * - we do NOT check if the placeholder is implemented by addressbook or a valid custom-field name! * - "test" or "{test}}" are NOT valid */ static EMAIL_PLACEHOLDER_PREG = new RegExp('^(.*{{#?[a-z0-9_]+}}.*|'+IsEmail.EMAIL_PREG.source.substr(1, IsEmail.EMAIL_PREG.source.length-2)+')$', 'i'); /** * * @param _allowPlaceholders true: allow valid email-addresses OR something with placeholder(s) */ constructor(_allowPlaceholders: boolean) { super(_allowPlaceholders ? IsEmail.EMAIL_PLACEHOLDER_PREG : IsEmail.EMAIL_PREG); } /** * Give a message about this field being required. Could be customised according to MessageData. * @param {MessageData | undefined} data * @returns {Promise} */ static async getMessage(data) { return data.formControl.egw().lang("Invalid email") + (data.modelValue ? ' "' + data.modelValue + '"' : ""); } }