验证不适用于3层

时间:2014-08-22 15:07:00

标签: c# asp.net regex validation

我的DAT / DAL班级代码很好。 在我的业务逻辑层中..我想使用Regex验证来验证ID号。

private int idno;
    public int IDNO
    {
        get { return idno; }
        set
        {

            Regex r = new Regex("^[a-zA-Z ]+$");
            if(r.IsMatch(idno.ToString()))
                throw new Exception("Invalid length");
            idno = value;


        }

     public void AddStudent()
    {
        DAT.AddStudent(this.idno);
    }
     public DataTable ViewStudent()
    {
        return DAT.ViewStudent();
    }

然后在我的表示层中,我在“添加”按钮中调用了业务逻辑层

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            BIZ b = new BIZ();
            b.IDNO = int.Parse(TextBox1.Text.ToString());


            b.AddStudent();

            GridView1.DataSource = b.ViewStudent();
            GridView1.DataBind();
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

验证始终是“输入字符串格式不正确”。我希望验证将是我在BLL中编写的内容。我的意思是在我的BLL中,如果用户键入字符串但它为什么不抛出BLL异常,它已经验证了?

1 个答案:

答案 0 :(得分:0)

您似乎没有对该属性的setter执行任何验证。它应该读过吗?

            Regex r = new Regex("^[a-zA-Z ]+$");
            if(r.IsMatch(value.ToString()))
                throw new Exception("Invalid length");
            this.idno = value;
相关问题