2022-03-08 19:19:33 +01:00
|
|
|
import {Pattern} from "@lion/form-core";
|
|
|
|
|
|
|
|
export class IsEmail extends Pattern
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Regexes for validating email addresses incl. email in angle-brackets eg.
|
|
|
|
* + "Ralf Becker <rb@egroupware.org>"
|
|
|
|
* + "Ralf Becker (EGroupware GmbH) <rb@egroupware.org>"
|
|
|
|
* + "<rb@egroupware.org>" or "rb@egroupware.org"
|
|
|
|
* + '"Becker, Ralf" <rb@egroupware.org>'
|
|
|
|
* + "'Becker, Ralf' <rb@egroupware.org>"
|
|
|
|
* but NOT:
|
|
|
|
* - "Becker, Ralf <rb@egroupware.org>" (contains comma outside " or ' enclosed block)
|
|
|
|
* - "Becker < Ralf <rb@egroupware.org>" (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 "(?<![.\s])", used to check that name-part does not end in
|
|
|
|
* a dot or white-space. The expression is valid in recent Chrome, but fails
|
|
|
|
* eg. in Safari 11.0 or node.js 4.8.3 and therefore grunt uglify!
|
|
|
|
* Server-side will fail in that case because it uses the full regexp.
|
|
|
|
*/
|
2022-09-12 14:50:52 +02:00
|
|
|
static EMAIL_PREG : RegExp = /^(([^\042',<][^,<]+|\042[^\042]+\042|\'[^\']+\'|"(?:[^"\\]|\\.)*")\s?<)?[^\x00-\x20()\xe2\x80\x8b<>@,;:\042\[\]\x80-\xff]+@([a-z0-9ÄÖÜäöüß](|[a-z0-9ÄÖÜäöüß_-]*[a-z0-9ÄÖÜäöüß])\.)+[a-z]{2,}>?$/i;
|
2022-03-08 19:19:33 +01:00
|
|
|
|
2023-04-11 15:01:40 +02:00
|
|
|
/**
|
|
|
|
* Allow everything containing at least one placeholder e.g.:
|
|
|
|
* - "{{email}}"
|
|
|
|
* - "{{n_fn}} <{{email}}"
|
|
|
|
* - "{{#<custom-field-name>}}"
|
|
|
|
* - 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)
|
2022-03-08 19:19:33 +01:00
|
|
|
{
|
2023-04-11 15:01:40 +02:00
|
|
|
super(_allowPlaceholders ? IsEmail.EMAIL_PLACEHOLDER_PREG : IsEmail.EMAIL_PREG);
|
2022-03-08 19:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Give a message about this field being required. Could be customised according to MessageData.
|
|
|
|
* @param {MessageData | undefined} data
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
|
|
|
static async getMessage(data)
|
|
|
|
{
|
2022-09-23 18:08:31 +02:00
|
|
|
return data.formControl.egw().lang("Invalid email") + (data.modelValue ? ' "' + data.modelValue + '"' : "");
|
2022-03-08 19:19:33 +01:00
|
|
|
}
|
|
|
|
}
|