首页 > 解决方案 > 从验证函数验证 wpf 中文本框中的数据

问题描述

好吧,我有以下问题我尝试验证我所在国家/地区提供者的 RUC,问题是我已经实现了以下验证功能:

身份验证功能

 public class Verifica_Identificacion
{
    internal static bool VerificaIdentificacion(string identificacion)
    {
        bool estado = false;
        char[] valced = new char[13];
        int provincia;
        if (identificacion.Length >= 10)
        {
            valced = identificacion.Trim().ToCharArray();
            provincia = int.Parse((valced[0].ToString() + valced[1].ToString()));
            if (provincia > 0 && provincia < 25)
            {
                if (int.Parse(valced[2].ToString()) < 6)
                {
                    estado = VC.VerificaCedula(valced);
                }
                else if (int.Parse(valced[2].ToString()) == 6)
                {
                    estado = VSP.VerificaSectorPublico(valced);
                }
                else if (int.Parse(valced[2].ToString()) == 9)
                {

                    estado = VPJ.VerificaPersonaJuridica(valced);
                }
            }
        }
        return estado;
    }
}

此功能依赖于独立执行的其他验证,因为存在某些类型的 RUC,下面显示了其他三个功能

身份验证

public class VC
{
    public static bool VerificaCedula(char[] validarCedula)
    {
        int aux = 0, par = 0, impar = 0, verifi;
        for (int i = 0; i < 9; i += 2)
        {
            aux = 2 * int.Parse(validarCedula[i].ToString());
            if (aux > 9)
                aux -= 9;
            par += aux;
        }
        for (int i = 1; i < 9; i += 2)
        {
            impar += int.Parse(validarCedula[i].ToString());
        }

        aux = par + impar;
        if (aux % 10 != 0)
        {
            verifi = 10 - (aux % 10);
        }
        else
            verifi = 0;
        if (verifi == int.Parse(validarCedula[9].ToString()))
            return true;
        else
            return false;
    }
}

公共部门核查

public class VSP
{
    public static bool VerificaSectorPublico(char[] validarCedula)
    {
        int aux = 0, prod, veri;
        veri = int.Parse(validarCedula[9].ToString()) + int.Parse(validarCedula[10].ToString()) + int.Parse(validarCedula[11].ToString()) + int.Parse(validarCedula[12].ToString());
        if (veri > 0)
        {
            int[] coeficiente = new int[8] { 3, 2, 7, 6, 5, 4, 3, 2 };

            for (int i = 0; i < 8; i++)
            {
                prod = int.Parse(validarCedula[i].ToString()) * coeficiente[i];
                aux += prod;
            }

            if (aux % 11 == 0)
            {
                veri = 0;
            }
            else if (aux % 11 == 1)
            {
                return false;
            }
            else
            {
                aux = aux % 11;
                veri = 11 - aux;
            }

            if (veri == int.Parse(validarCedula[8].ToString()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}

法人验证

public class VPJ
{
    public static bool VerificaPersonaJuridica(char[] validarCedula)
    {
        int aux = 0, prod, veri;
        veri = int.Parse(validarCedula[10].ToString()) + int.Parse(validarCedula[11].ToString()) + int.Parse(validarCedula[12].ToString());
        if (veri > 0)
        {
            int[] coeficiente = new int[9] { 4, 3, 2, 7, 6, 5, 4, 3, 2 };
            for (int i = 0; i < 9; i++)
            {
                prod = int.Parse(validarCedula[i].ToString()) * coeficiente[i];
                aux += prod;
            }
            if (aux % 11 == 0)
            {
                veri = 0;
            }
            else if (aux % 11 == 1)
            {
                return false;
            }
            else
            {
                aux = aux % 11;
                veri = 11 - aux;
            }

            if (veri == int.Parse(validarCedula[9].ToString()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}

我想要的是在一个文本框中使用“ID 验证功能”,该文本框中接受 RUC 作为输入到目前为止的数据输入并存储在数据库中我这样做如下,但是,如您所见,我只能输入没有相应验证的 ruc 字符串。在输入ruc时需要帮助使用我的验证功能并保存,如果输入正确,会出现“Provider stored correct”消息,如果输入不成功,退出消息“Wrong RUC re -输入数据”

存储过程的数据输入代码(数据层)

  public void Insertar(string NombreProveedor, string rucProveedor)
    {
        comando.Connection = con.AbrirConexion();
        comando.CommandText = "IngresarProveedor";
        comando.CommandType = CommandType.StoredProcedure;
        comando.Parameters.AddWithValue("@NombreProveedor", NombreProveedor);
        comando.Parameters.AddWithValue("@rucProveedor", rucProveedor);
        comando.ExecuteNonQuery();
        comando.Parameters.Clear();
        con.CerrarConexion();

    }

数据录入代码(商务披风)

private CD_RUC proveedor_cd = new CD_RUC();
    public void InsertarProv(string NombreProveedor, string rucProveedor)
    {
        proveedor_cd.Insertar(NombreProveedor, rucProveedor);

    }

保存按钮设置(表示层)

 private void btn_Guardar_Click(object sender, RoutedEventArgs e)
    {
        proveedorcn NPro = new proveedorcn();
        NPro.InsertarProv(TXT_Nombre_Proveedor_P.Text, TXT_RUC.Text);
        MessageBox.Show("Proveedor Ingresado Correctamente");
    }

标签: c#wpfvisual-studio

解决方案


推荐阅读