Skip to content

Commit

Permalink
Fix: Better email verification when posted with forms
Browse files Browse the repository at this point in the history
Trim input form fields in tournament registration
  • Loading branch information
axunonb committed Mar 17, 2023
1 parent ad47c4e commit a693ae6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ClubSite/ClubSite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Version>3.5.2</Version>
<Version>3.5.3</Version>
<Authors>axuno gGmbH</Authors>
<Description>The source code for https://www.volleyballclub.de/</Description>
<CurrentYear>$([System.DateTime]::Now.ToString(yyyy))</CurrentYear>
Expand Down
24 changes: 24 additions & 0 deletions ClubSite/Library/EmailValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) axuno gGmbH and Contributors.
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/axuno/ClubSite
//

namespace ClubSite.Library
{
public static class EmailValidator
{
public static bool IsValid(string? email)
{
try
{
_ = new MimeKit.MailboxAddress(email, email);
return true;
}
catch
{
return false;
}
}
}
}
2 changes: 2 additions & 0 deletions ClubSite/Pages/ContactPage.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public async Task<IActionResult> OnPostAsync(Guid id, bool draft, CancellationTo

if (Captcha != HttpContext.Session.GetString(CaptchaSvgGenerator.CaptchaSessionKeyName) && !string.IsNullOrEmpty(Captcha))
ModelState.AddModelError(nameof(Captcha), "Ergebnis der Rechenaufgabe ist nicht korrekt");
if(!EmailValidator.IsValid(Email))
ModelState.AddModelError($"{nameof(Email)}", $"'{nameof(Email)}' enthält keine gültige E-Mail Adresse");

if (!ModelState.IsValid) return Page();

Expand Down
7 changes: 7 additions & 0 deletions ClubSite/Pages/TournamentRegistration.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public async Task<IActionResult> OnPostAsync(CancellationToken cancellationToken
ModelState.Clear();

await TryUpdateModelAsync(new {Registration});
if(!EmailValidator.IsValid(Registration.Email))
ModelState.AddModelError($"{nameof(Registration)}.{nameof(Registration.Email)}", $"'{nameof(Registration.Email)}' enthält keine gültige E-Mail Adresse");
if (Captcha != HttpContext.Session.GetString(CaptchaSvgGenerator.CaptchaSessionKeyName) &&
!string.IsNullOrEmpty(Captcha))
ModelState.AddModelError(nameof(Captcha), "Ergebnis der Rechenaufgabe war nicht korrekt");
Expand All @@ -167,6 +169,11 @@ public async Task<IActionResult> OnPostAsync(CancellationToken cancellationToken
if (isNewRegistration)
{
// New record
Registration.Email = Registration.Email?.Trim();
Registration.FirstName = Registration.FirstName?.Trim();
Registration.LastName = Registration.LastName?.Trim();
Registration.TeamName = Registration.TeamName?.Trim();
Registration.ClubName = Registration.ClubName?.Trim();
Registration.RegistrationId = Guid.NewGuid();
Registration.CreatedOn = Registration.ModifiedOn = Registration.RegisteredOn = now;
Registration.TournamentDate = new DateTime(TournamentDate);
Expand Down

0 comments on commit a693ae6

Please sign in to comment.