quarta-feira, 7 de maio de 2014

Classe com Validação de CPF - CEP e EMAIL

Helder Ricardo Silva
Analista de Sistemas - Universidade Braz Cubas
 
Segue abaixo a classe com atributos - propriedades - métodos
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics.Contracts;
using System.Windows.Forms;
namespace DataDogAndCatShop.Model
{
    public class  Cliente
    {
        protected string nome;
        private string cpf;
        private string rg;
        private string telefone;
        private string celular;
        private string email;
        private string cep;
        private string endereco;
        private string bairro;
        private string cidade;
        private string estado;
        private string pontoref;
        private string dataNasc;
        private string dataUltm;
        private int numero;
        //propriedades da tabela
        //propriedade para nome
        public string _nome
        {
            get
            {
                return nome;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    MessageBox.Show("Não é possível apresentar nome vazio");
                }
                else
                {
                   nome= value;
                }
                Contract.EndContractBlock();
            }
        }
        public string _cpf
        {
            get
            {
                return cpf;
            }
            set
            {
                if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
                {
                    MessageBox.Show("O CPF não pode ser vazio");
                }
                else
                {
                    cpf = value;
                }
            }
        }
        public string _rg
        {
            get
            {
                return rg;
            }
            set
            {
                if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
                {
                    MessageBox.Show("Não é possível rg vazio");
                }
                else
                {
                    rg = value;
                }
            }
        }
        public string _telefone
        {
            get
            {
                return telefone;
            }
            set
            {
                telefone = value;
            }
        }
        public string _celular
        {
            get
            {
                return celular;
            }
            set
            {
                celular = value;
            }
        }
        public string _email
        {
            get
            {
                return email;
            }
            set
            {
                email = value;
            }
        }
        public string _cep
        {
            get
            {
                return cep;
            }
            set
            {
                cep = value;
            }
        }
        public string _endereco
        {
            get
            {
                return endereco;
            }
            set
            {
                if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
                {
                    MessageBox.Show("Endereço não pode estar vazio");
                }
                else
                {
                    endereco = value;
                }
            }
        }
        public int _numero
        {
            get
            {
                return numero;
            }
            set
            {
                if (string.IsNullOrEmpty(value.ToString()) || string.IsNullOrWhiteSpace(value.ToString()))
                {
                    MessageBox.Show("O campo número não pode estar vazio");
                }
                else
                {
                    numero = value;
                }
            }
        }
        public string _bairro
        {
            get
            {
                return bairro;
            }
            set
            {
                if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
                {
                    MessageBox.Show("Não é possível bairro vazio");
                }
                else
                {
                    bairro = value;
                }
            }
        }
        public string _cidade
        {
            get
            {
                return cidade;
            }
            set
            {
                if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
                {
                    MessageBox.Show("Não é possível cidade vazia");
                }
                else
                {
                    cidade = value;
                }
            }
        }
        public string _estado
        {
            get
            {
                return estado;
            }
            set
            {
                if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
                {
                    MessageBox.Show("Não é possível estado vazio");
                }
                else
                {
                    estado = value;
                }
            }
        }
        public string _pontoref
        {
            get
            {
                return pontoref;
            }
            set
            {
                pontoref = value;
            }
        }
        public string _dataNasc
        {
            get
            {
                return dataNasc;
            }
            set
            {
                dataNasc = value;
            }
        }
        public string _dataUltm
        {
            get
            {
                return dataUltm;
            }
            set
            {
                if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
                {
                    MessageBox.Show("Não é possível Data da Última Visita vazio");
                }
                else
                {
                    dataUltm = value;
                }
            }
        }
        public bool validarCPF(string CPF)
        {
            int[] mt1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
            int[] mt2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
            string TempCPF;
            string Digito;
            int soma;
            int resto;
 
            CPF = CPF.Trim();
            CPF = CPF.Replace(".", "").Replace("-", "");
 
            if (CPF.Length != 11)
                return false;
 
            TempCPF = CPF.Substring(0, 9);
            soma = 0;
            for (int i = 0; i < 9; i++)
                soma += int.Parse(TempCPF[i].ToString()) * mt1[i];
 
            resto = soma % 11;
            if (resto < 2)
                resto = 0;
            else
                resto = 11 - resto;
 
            Digito = resto.ToString();
            TempCPF = TempCPF + Digito;
            soma = 0;
 
            for (int i = 0; i < 10; i++)
                soma += int.Parse(TempCPF[i].ToString()) * mt2[i];
 
            resto = soma % 11;
            if (resto < 2)
                resto = 0;
            else
                resto = 11 - resto;
 
            Digito = Digito + resto.ToString();
 
            return CPF.EndsWith(Digito);
        }
        public bool ValidaCep(string cep)
        {
            if (cep.Length == 8)
            {
                cep = cep.Substring(0, 5) + "-" + cep.Substring(5, 3);
                //txt.Text = cep;
            }
            return System.Text.RegularExpressions.Regex.IsMatch(cep, ("[0-9]{5}-[0-9]{3}"));
        }
        //Método que valida o Email
        public  bool ValidaEmail(string email)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(email, ("(?<user>[^@]+)@(?<host>.+)"));
        }
    }
 }

   
   
 

Nenhum comentário:

Postar um comentário