---
title: "Find invalid email characters in C#"
url: https://recursiveai.org/onecontact/cookbook/find-invalid-email-character/
description: "Find where an email address fails to parse in C#. Use OneContact’s error offset and length to highlight the offending text in a form."
version: "1.0.0"
---

# Find invalid email characters in C#

**OneContact** is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneContact) · [GitHub](https://github.com/recursive-ai-oss/onecontact-csharp) · [Docs](https://recursiveai.org/onecontact/docs/) · [Download example (.zip)](https://recursiveai.org/onecontact/cookbook/find-invalid-email-character/find-invalid-email-character.zip)

Highlight the spot where parsing stopped.

```shell
dotnet add package RecursiveAI.OneContact
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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:

```text
1
```

Unzip [the example](https://recursiveai.org/onecontact/cookbook/find-invalid-email-character/find-invalid-email-character.zip), then `dotnet run`.

## Full program

```csharp
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:

```text
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:

```csharp
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);
```

```text
email.missing-at
```

## Related recipes

- [Validate an email address in C#](https://recursiveai.org/onecontact/cookbook/validate-email-address/)
- [Detect email addresses that require SMTPUTF8](https://recursiveai.org/onecontact/cookbook/detect-email-addresses-requiring-smtputf8/)

## 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](https://recursiveai.org/onecontact/docs/email/#what-is-read) 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](https://recursiveai.org/onecontact/docs/email/#reason-codes) 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.
