Files
ophelias-oasis/OpheliasOasis/Validation.cs

65 lines
2.6 KiB
C#

using System.Text.RegularExpressions;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
namespace Ophelias.Expressions
{
internal static class Expressions
{
/*
* These are a collection of regular expressions used in the validation class.
* They exist in their own class to better isolate them and make it easier to
* find.
*
* CardRx
* Regular expression to check for 16 digit credit cards
*
* ExpirationRx
* Ensures that a card expires sometime in past 2021
*
* CCVRx
* Checks to make sure the input is 3 digits
*
* MoneyRx
* Basic check to make sure money is in US format. 300.20/ 0.00 etc.
*/
internal static Regex CardRx = new(@"^[0-9]{16}$", RegexOptions.Compiled);
internal static Regex ExpriationRx = new(@"^(0?[1-9]|1[012])/[2-9][0-9]{1}$", RegexOptions.Compiled);
internal static Regex CCVRx = new(@"^[0-9]{3}$", RegexOptions.Compiled);
internal static Regex MoneyRx = new(@"^(\d+\.\d{2}|\d+)$", RegexOptions.Compiled);
}
internal static class Validation
{
internal static bool ValidateCreditCard(string CreditCard) // Returns if the regex evaluates true or false
{
return Expressions.CardRx.IsMatch(CreditCard);
}
internal static bool ValidateExpirationDate(string Expiration) // Returns if the regex evaluates true or false
{
if (Expressions.ExpriationRx.IsMatch(Expiration))
{
DateTime dt = DateTime.ParseExact(Expiration, "MM/yy", CultureInfo.InvariantCulture); // Converts to date time for comparison
if (dt.Date >= DateTime.Now.Date)
return true;
}
return false;
}
internal static bool ValidateEmail(string email) // Returns if the regex evaluates true or false
{
EmailAddressAttribute EmailChecker = new(); // Creates an email checker based off the EmailAddressAttribute class
return EmailChecker.IsValid(email);
}
internal static bool ValidateCCV(string CCV) // Returns if the regex evaluates true or false
{
if (Expressions.CCVRx.IsMatch(CCV))
return true;
return false;
}
internal static bool ValidateMoney(string Money) // Returns if the regex evaluates true or false
{
return Expressions.MoneyRx.IsMatch(Money);
}
}
}