Custom Attribute Oluşturma

TC kimlik numarası ve cep telefonu için alan doğrulama amaçlı yazdığım attribute’ler aşağıdadır.

Tckn.cs

using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace WpfValidation.CustomAttibutes
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    sealed public class Tckn : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }

            bool result = false;

            try
            {
                if (value.ToString().Length == 11)
                {
                    long tcNo = Int64.Parse(value.ToString());

                    long atcno = tcNo / 100;
                    long btcno = tcNo / 100;

                    long c1 = atcno % 10; atcno = atcno / 10;
                    long c2 = atcno % 10; atcno = atcno / 10;
                    long c3 = atcno % 10; atcno = atcno / 10;
                    long c4 = atcno % 10; atcno = atcno / 10;
                    long c5 = atcno % 10; atcno = atcno / 10;
                    long c6 = atcno % 10; atcno = atcno / 10;
                    long c7 = atcno % 10; atcno = atcno / 10;
                    long c8 = atcno % 10; atcno = atcno / 10;
                    long c9 = atcno % 10; atcno = atcno / 10;
                    long q1 = ((10 - ((((c1 + c3 + c5 + c7 + c9) * 3) + (c2 + c4 + c6 + c8)) % 10)) % 10);
                    long q2 = ((10 - (((((c2 + c4 + c6 + c8) + q1) * 3) + (c1 + c3 + c5 + c7 + c9)) % 10)) % 10);

                    result = ((btcno * 100) + (q1 * 10) + q2 == tcNo);
                }

                return result;
            }
            catch (Exception)
            {
                return false;
            }   
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name);
        }
    }
}

MobilePhone.cs

using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text.RegularExpressions;

namespace WpfValidation.CustomAttributes
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    sealed public class MobilePhone : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }

            try
            {
                var number = value.ToString();

                return Regex.IsMatch(value.ToString(), "^[0-9]{1" + (number.StartsWith("0") ? "1" : "0") + "}$");
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *