tryParse static method

Email? tryParse(
  1. String input, {
  2. int maxInputLength = 30,
})

Parse input, returning a valid email address.

This is based on the HTML Living Standard, section 4.10.5 The input element.

See: https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email)

The approach is a pragmatic subset of RFC 5322.

Implementation

static Email? tryParse(String input, {int maxInputLength = 30}) {
  if (input.length > maxInputLength) {
    return null;
  }

  var match = _emailRegex.firstMatch(input);
  if (match == null) {
    return null;
  }
  var localPart = match.namedGroup('localPart');
  var domain = match.namedGroup('domain');

  if (localPart == null || domain == null) {
    return null;
  }
  return Email._(localPart: localPart, domain: domain);
}