I have the following method:
public async Task<IActionResult> GetUsersAPICall([Bind(nameof(query))] [RegularExpression(@"^\w+(\.\w+)*@\w+(\.\w+)+|(\w+(\s\w+)*)|[a-z0-9]+$", ErrorMessage = "This does not look like a valid query")] string query)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
}
This fails for email addresses like test-def.ghi@de.jkl.com
(but not for email addresses like test@test.com
).
I know for a fact that the regex is the correct pattern, because the following LINQPad script displays True
as expected for the exact same email address:
void Main()
{
var r = new Regex(@"^\w+(\.\w+)*@\w+(\.\w+)+|(\w+(\s\w+)*)|[a-z0-9]+$");
r.IsMatch("test-def.ghi@de.jkl.com").Dump();
}
I tried dropping the [Bind(nameof(query))]
entirely, but that didn't help.
If I change this to
public async Task<IActionResult> GetUsersAPICall([Bind(nameof(query))] string query)
{
if (!Regex.IsMatch(query, @"^\w+(\.\w+)*@\w+(\.\w+)+|(\w+(\s\w+)*)|[a-z0-9]+$"))
{
return BadRequest();
}
// ...
}
I'm completely baffled as to why what I had originally wasn't working. Can someone see what I'm missing here?
The closest I've found so far on Stack Overflow is this Q&A, but there it turned out that the OP's regex was wrong (which isn't the case for me, since the Regex works just fine in two different contexts).