Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created September 10, 2020 17:22
Show Gist options
  • Save karenpayneoregon/cfb79ad47f8dab8edc3213b03ddf8dbc to your computer and use it in GitHub Desktop.
Save karenpayneoregon/cfb79ad47f8dab8edc3213b03ddf8dbc to your computer and use it in GitHub Desktop.
Validate SSN
using System.Text.RegularExpressions;
namespace Validators
{
public static class ValidateExtensions
{
/// <summary>
/// Validate
/// Don't allow "219-09-999" or "078-05-1120" explicitly
/// Don't allow the SSN to begin with 666, 000 or anything between 900-999
/// Explicit dash (separating Area and Group numbers)
/// Don't allow the Group Number to be "00"
/// Another dash (separating Group and Serial numbers)
/// Don't allow last four digits to be "0000"
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsValidSsnWithoutDashes(this string value) =>
Regex.IsMatch(value.CleanSsn(),
@"^(?!\b(\d)\1+\b)(?!123456789|219099999|078051120)(?!666|000|9\d{2})\d{3}(?!00)\d{2}(?!0{4})\d{4}$");
/// <summary>
/// Simple validate 9 digits
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsValidSsnSimple(this string value)
{
var regexItem = new Regex(@"^\d{9}$");
var matcher = regexItem.Match(value);
return matcher.Success;
}
public static string CleanSsn(this string ssn) => ssn.Replace("-", "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment