Kuwait Civil ID Validator


In the following solution, I had implemented a custom Enterprise Library Validator for Kuwait Civil ID.

The validation logic has been acquired from PACI as the following:

1 /// <summary> 2 /// Represent the associated validator attribute for <see cref="CivilIdValidator"/> 3 /// </summary> 4 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] 5 public class CivilIdValidatorAttribute : ValidatorAttribute 6 { 7 /// <summary> 8 /// Creates the <see cref="T:Microsoft.Practices.EnterpriseLibrary.Validation.Validator" /> described by the attribute object providing validator specific 9 /// information. 10 /// </summary> 11 /// <param name="targetType">The type of object that will be validated by the validator.</param> 12 /// <returns> 13 /// The created <see cref="T:Microsoft.Practices.EnterpriseLibrary.Validation.Validator" />. 14 /// </returns> 15 /// <remarks> 16 /// This operation must be overriden by subclasses. 17 /// </remarks> 18 protected override Validator DoCreateValidator(Type targetType) 19 { 20 return new CivilIdValidator(); 21 } 22 }

and the validator class:

1 /// <summary> 2 /// MS EL Custom Validation Attribute for Civil ID validation 3 /// </summary> 4 public class CivilIdValidator : Validator<decimal> 5 { 6 7 8 /// <summary> 9 /// Initializes a new instance of the <see cref="CivilIdValidator"/> class. 10 /// </summary> 11 public CivilIdValidator() : base("","") { } 12 13 /// <summary> 14 /// Does the validate. 15 /// </summary> 16 /// <param name="objectToValidate">The object to validate.</param> 17 /// <param name="currentTarget">The current target.</param> 18 /// <param name="key">The key.</param> 19 /// <param name="validationResults">The validation results.</param> 20 protected override void DoValidate(decimal objectToValidate, object currentTarget, string key, ValidationResults validationResults) 21 { 22 if(!Utilities.IsValidCivilId(objectToValidate.ToString())) 23 { 24 LogValidationResult(validationResults, "???? ????? ??? ???? ???? ?? 12 ???", currentTarget, key); 25 return; 26 } 27 } 28 29 /// <summary> 30 /// Gets the message template to use when logging results no message is supplied. 31 /// </summary> 32 protected override string DefaultMessageTemplate 33 { 34 get 35 { 36 return ""; 37 } 38 } 39 40 41 }

The validation logic:

1 /// <summary> 2 /// Custom utilities used by the applciation 3 /// </summary> 4 public class Utilities 5 { 6 /// <summary> 7 /// Determines whether [is valid civil identifier] [the specified civil identifier]. 8 /// </summary> 9 /// <param name="civilId">The civil identifier.</param> 10 /// <returns></returns> 11 public static bool IsValidCivilId(string civilId) 12 { 13 if (string.IsNullOrEmpty(civilId)) 14 return false; 15 16 if (civilId.Length != 12) 17 return false; 18 19 decimal civilIdDecimal; 20 21 if (!decimal.TryParse(civilId, out civilIdDecimal)) 22 return false; 23 24 int calculation; 25 26 int monthPart = int.Parse(civilId.Substring(3,2)); 27 int dayPart = int.Parse(civilId.Substring(3,2)); 28 29 30 if ((monthPart > 12 || monthPart <1) || (dayPart > 31 || dayPart <1)) 31 { 32 return false; 33 } 34 35 calculation = 2 * int.Parse(civilId.Substring(0,1)) + 1 * int.Parse(civilId.Substring(1, 1)) + 6 * int.Parse(civilId.Substring(2, 1)) + 3 * int.Parse(civilId.Substring(3, 1)) + 7 * int.Parse(civilId.Substring(4, 1)) + 9 * int.Parse(civilId.Substring(5, 1)) + 10 * int.Parse(civilId.Substring(6, 1)) + 5 * int.Parse(civilId.Substring(7, 1)) + 8 * int.Parse(civilId.Substring(8, 1)) + 4 * int.Parse(civilId.Substring(9, 1)) + 2 * int.Parse(civilId.Substring(10, 1)); 36 37 calculation = calculation % 11; 38 39 calculation = 11 - calculation; 40 41 if (calculation != int.Parse(civilId.Substring(11, 1))) 42 return false; 43 else 44 return true; 45 } 46 }

I hope that helped.

Ahmed

Advertisements

One thought on “Kuwait Civil ID Validator

  1. Again, it’s about supply and demand for each individual ore.
    It’s quite popular and I was curious but I couldn’t find any reviews
    that gave an overview of the guide and its contents. Fishing
    Facts Manual – Now this is what you call a fishing guide.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s