Highlight the spot where parsing stopped.
dotnet add package RecursiveAI.OneContactRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System.Buffers; using RecursiveAI.OneContact.Email; var status = EmailParser.TryParse( "a..b@example.com", out _, out var detail); if (status == OperationStatus.InvalidData) Console.WriteLine( detail.Issues[0].Span.Start);
It prints the offset of the first dot:
1Unzip the example, then dotnet run.
Full program
using System.Buffers; using RecursiveAI.OneContact.Email; string input = " a..b@example.com"; const string label = "Input: "; var status = EmailParser.TryParse(input, out _, out var detail); if (status == OperationStatus.InvalidData) { var issue = detail.Issues[0]; Console.WriteLine(label + input); if (issue.Span.Length > 0) { Console.WriteLine(new string(' ', label.Length + issue.Span.Start) + new string('^', issue.Span.Length)); } Console.WriteLine(issue.Reason.Id); }
dotnet run prints:
Input: a..b@example.com ^ email.invalid-local-part
The input includes two leading spaces. The diagnostic points into that original string, not a trimmed copy. A failure can also cover a range or have length zero, so for form feedback select the reported range rather than always selecting one character.
Variations
PrimaryReason returns the first error’s reason, so on a failed email parse it matches Issues[0].Reason.
Use the reason code for a missing @, where there’s no offending character to select:
using System.Buffers; using RecursiveAI.OneContact.Email; var status = EmailParser.TryParse( "not-an-email", out _, out var detail); if (status == OperationStatus.InvalidData) Console.WriteLine(detail.PrimaryReason.Id);
email.missing-atRelated recipes
The short answer
Use EmailParser.TryParse when a Boolean isn’t enough. On InvalidData, the parser provides one error issue. Read its Reason.Id for what went wrong and Span.Start and Span.Length for where it happened. Apply the span to the same text you passed to the parser. For a form, pair the selection with a message such as “Remove the extra dot before the @” rather than showing the raw reason code alone.
What are invalid email address characters?
It depends on where the character appears. The dot in a.b@example.com is allowed, but the adjacent dots in a..b@example.com break the unquoted local part, the text before the @. A space can be accepted inside a quoted local part such as "a b"@example.com; that doesn’t make an unquoted space valid. The email grammar OneContact reads includes both quoted local parts and internationalized domains, so a list of forbidden characters would reject some valid inputs and miss some invalid combinations.
How do I identify what’s wrong with an email address?
Use the parser’s reason codes and offsets together. For the double-dot example, the reason is email.invalid-local-part and the span marks a dot. For email.missing-at, explain what is missing; selecting one character wouldn’t help. For an empty field, show a required-field message instead of underlining a character. A successful parse can still include a warning, so use the returned status to decide whether syntax parsing failed.
Start and Length count UTF-16 code units, the same units a C# string uses for slicing. They are not byte offsets or counts of visible characters: an emoji can occupy two code units. If you trim or otherwise transform the input yourself before parsing, the offsets refer to that transformed text. Pass the form value as entered if you want to select text in the original field.