输入验证函数

csharp
阅读 42 收藏 0 点赞 0 评论 0

inputsValidations
//verifica string email valida (RFC822)
        public bool IsValidEmail(string email)
        {
            try
            {
                var addr = new System.Net.Mail.MailAddress(email);
                return addr.Address == email;
            }
            catch
            {
                return false;
            }
        }
        
        
-------------------------------------------------------------------        



public bool ValidaNumeroDocumento(string tipoDoc, string numDocComp)
        {                        
            if (tipoDoc.Equals(ConfigurationManager.AppSettings["tipoDocBI"])) {
                numDocComp = numDocComp.Replace("-", "").Replace(".", "").Replace(",", "").Replace(" ", "");
                if (numDocComp.Length == 9)
                {
                    //numDoc = numDoc.PadLeft(8, '0');
                    string numDoc = numDocComp.Substring(0, 8);
                    string digitCont = numDocComp.Substring(8, 1);
                    int i = 9;
                    int sum = 0;
                    foreach (char c in numDoc)
                    {
                        sum += (c - '0') * i;
                        i--;
                    }
                    if ((sum % 11 == 1 || sum % 11 == 0) && (Convert.ToInt32(digitCont) == 0))
                    {
                        return true;
                    }
                    else
                    {
                        if (11 - (sum % 11) == Convert.ToInt32(digitCont))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }

                }
                else
                {
                    return false;
                }
            }
            else if (tipoDoc.Equals(ConfigurationManager.AppSettings["tipoDocCC"]))
            {
                numDocComp = numDocComp.Replace("-", "").Replace(".", "").Replace(",", "").Replace(" ", "");
                if (numDocComp.Length == 12)
                {
                    Dictionary<string, int> dicLetras = new Dictionary<string, int>();
                    dicLetras.Add("A", 10);
                    dicLetras.Add("B", 11);
                    dicLetras.Add("C", 12);
                    dicLetras.Add("D", 13);
                    dicLetras.Add("E", 14);
                    dicLetras.Add("F", 15);
                    dicLetras.Add("G", 16);
                    dicLetras.Add("H", 17);
                    dicLetras.Add("I", 18);
                    dicLetras.Add("J", 19);
                    dicLetras.Add("K", 20);
                    dicLetras.Add("L", 21);
                    dicLetras.Add("M", 22);
                    dicLetras.Add("N", 23);
                    dicLetras.Add("O", 24);
                    dicLetras.Add("P", 25);
                    dicLetras.Add("Q", 26);
                    dicLetras.Add("R", 27);
                    dicLetras.Add("S", 28);
                    dicLetras.Add("T", 29);
                    dicLetras.Add("U", 30);
                    dicLetras.Add("V", 31);
                    dicLetras.Add("W", 32);
                    dicLetras.Add("X", 33);
                    dicLetras.Add("Y", 34);
                    dicLetras.Add("Z", 35);
                    
                    int soma = Convert.ToInt32(numDocComp.Substring(11, 1))
                                + (dicLetras[numDocComp.Substring(10, 1).ToUpper()] * 2 > 9 ? (dicLetras[numDocComp.Substring(10, 1).ToUpper()] * 2) - 9 : dicLetras[numDocComp.Substring(10, 1).ToUpper()] * 2)
                                + dicLetras[numDocComp.Substring(9, 1).ToUpper()]
                                + (Convert.ToInt32(numDocComp.Substring(8, 1)) * 2 > 9 ? (Convert.ToInt32(numDocComp.Substring(8, 1)) * 2) - 9 : Convert.ToInt32(numDocComp.Substring(8, 1)) * 2)
                                + Convert.ToInt32(numDocComp.Substring(7, 1))
                                + (Convert.ToInt32(numDocComp.Substring(6, 1)) * 2 > 9 ? (Convert.ToInt32(numDocComp.Substring(6, 1)) * 2) - 9 : Convert.ToInt32(numDocComp.Substring(6, 1)) * 2)
                                + Convert.ToInt32(numDocComp.Substring(5, 1))
                                + (Convert.ToInt32(numDocComp.Substring(4, 1)) * 2 > 9 ? (Convert.ToInt32(numDocComp.Substring(4, 1)) * 2) - 9 : Convert.ToInt32(numDocComp.Substring(4, 1)) * 2)
                                + Convert.ToInt32(numDocComp.Substring(3, 1))
                                + (Convert.ToInt32(numDocComp.Substring(2, 1)) * 2 > 9 ? (Convert.ToInt32(numDocComp.Substring(2, 1)) * 2) - 9 : Convert.ToInt32(numDocComp.Substring(2, 1)) * 2)
                                + Convert.ToInt32(numDocComp.Substring(1, 1))
                                + (Convert.ToInt32(numDocComp.Substring(0, 1)) * 2 > 9 ? (Convert.ToInt32(numDocComp.Substring(0, 1)) * 2) - 9 : Convert.ToInt32(numDocComp.Substring(0, 1)) * 2);                    
                    if (soma % 10 == 0) return true;
                    else return false;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }            
        }
        
        
        
        
-------------------------------------------------------------


public bool ValidaTelemovel(string numero)
        {
            return Regex.Match(numero, @"^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$").Success; // Nao tem em conta o numero de digitos
            //@"\(?\d{3}\)?-? *\d{3}-? *-?\d{3}"
            //@"\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{3})"
        }
        
        
        
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号