Files
Common-VS2019/SacsStandardBase/Base/Helpers/SacsDataValidationHelper.cs
T
2021-05-05 15:36:28 +01:00

88 lines
3.0 KiB
C#

using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Text.RegularExpressions;
namespace Sarand.Net.Standard.Base.Helpers
{
//=======================================================================
public static class SacsDataValidationHelper
{
//-------------------------------------------------------------------
/// <summary>
/// Validate the email address.
/// <para>Ref. https://stackoverflow.com/questions/1365407/c-sharp-code-to-validate-email-address</para>
/// </summary>
public static bool IsValidEmailAddress(String value)
{
if (StringUtil.IsEmpty(value)) return false;
if (!new EmailAddressAttribute().IsValid(value)) return false;
// Exclude spaces.
if (Regex.Matches(value, " ").Count > 0) return false;
// Exclude consecutive dots.
if (Regex.Matches(value, "\\.\\.").Count > 0) return false;
// Check for at least one dot after @ [to ensure john@smith is invalid].
if (Regex.Matches(value, "@[^@.]+\\.").Count < 1) return false;
return true;
}
//-------------------------------------------------------------------
/// <summary>
/// Validate the phone number.
/// <para>Ref. https://www.twilio.com/blog/validating-phone-numbers-effectively-with-c-and-the-net-frameworks</para>
/// </summary>
public static bool IsValidPhone(String value)
{
if (StringUtil.IsEmpty(value)) return false;
if (!new PhoneAttribute().IsValid(value)) return false;
// Apply minimum length.
if (StringUtil.GetLength(value) < 8) return false;
return true;
}
//-------------------------------------------------------------------
// Tax identification number.
// European country Tax Identification Numbers:
// https://ec.europa.eu/taxation_customs/vies/faqvies.do#item_11
//-------------------------------------------------------------------
/// <summary>
/// Validate the tax identification number.
/// </summary>
public static bool IsValidTaxIdentificationNumber(String countryCode, String value)
{
if (StringUtil.IsEmpty(value)) return false;
//---------------------------------------------------------------
// GB, United Kingdom.
// Standard:
// GB999 9999 99 block of 3 digits, block of 4 digits and block of 2 digits [00-96]
// Branch:
// GB999 9999 99 9995 as above followed by block of 3 digits
// Government departments:
// GBGD9996 'GD' followed by block of 3 or 4 digits
// Health Authoritites:
// GBHA9997 'HA' followed by block of 3 or 4 digits
//---------------------------------------------------------------
// Apply minimum length.
if (StringUtil.GetLength(value) < 8) return false;
// Country prefix.
if (!value.StartsWith("GB")) return false;
return true;
}
//-------------------------------------------------------------------
}
//=======================================================================
}