public static void Main()
string serviceCode = "625";
bool offlineAuth = false;
bool deviceOnlinePin = false;
Console.WriteLine(string.Format("Validating service code: {0}", serviceCode));
var validator = new ServiceCodeValidator();
var firstCharResult = validator.IsFirstDigitValid(offlineAuth, serviceCode);
var secondCharResult = validator.IsSecondDigitValid(offlineAuth, serviceCode);
var thirdCharResult = validator.IsThirdDigitValid(offlineAuth, deviceOnlinePin, serviceCode);
if(firstCharResult || secondCharResult || thirdCharResult)
Console.WriteLine(string.Format("service code character 1 is valid: {0}", firstCharResult));
Console.WriteLine(string.Format("service code character 2 is valid: {0}", secondCharResult));
Console.WriteLine(string.Format("service code character 3 is valid: {0}", thirdCharResult));
Console.WriteLine(string.Format("service code: {0} is valid", serviceCode));
public class ServiceCodeValidator
public bool IsFirstDigitValid(bool isOfflineeAuthorisation, string serviceCode)
return !serviceCodeSearch(isOfflineeAuthorisation, serviceCode[0], new[] { '0', '3', '4', '5', '6', '7', '8', '9' });
public bool IsSecondDigitValid(bool isOfflineeAuthorisation, string serviceCode)
return !serviceCodeSearch(isOfflineeAuthorisation, serviceCode[1], new[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' });
public bool IsThirdDigitValid(bool isOfflineeAuthorisation,bool isOnlinePinVerificationAvailable, string serviceCode)
var thirdDigit = serviceCode[2];
if (thirdDigit == '0' && !isOnlinePinVerificationAvailable)
if (serviceCodeSearch(isOfflineeAuthorisation, thirdDigit , new[] { '8', '9' }))
return !serviceCodeSearch(true, serviceCode[2], new[] { '3', '4', '5' });
private Func<bool, char, char[], bool> serviceCodeSearch = (isOffline, match, search) =>
search.ToList().ForEach(s =>