简体   繁体   English

必填字段验证器允许提交空白文本框

[英]Required field validator allows empty textbox to be submitted

I have a required field validator for two textboxes for a submission form. 我有一个用于提交表单的两个文本框的必填字段验证器。 My code for the form is below. 我的表格代码如下。 For some odd reason it doesn't validate and allows the empty string to be entered into the database. 由于某种奇怪的原因,它无法验证,并允许将空字符串输入数据库。 The fields that are being entered are char but it doesn't submit it as null, just blank. 输入的字段为char,但不会将其提交为null,而是空白。

Front-end 前端

    <td style="width: 315px; height: 22px;border-right: solid 1px #a32020;">
                    <asp:TextBox ID="txtCompanyName" runat="server" BorderColor="#A32020" style ="border-right: solid 1px #a32020;" OnTextChanged="txtCompanyName_TextChanged" CausesValidation="True"></asp:TextBox>
                &nbsp;<asp:RequiredFieldValidator ID="rfvCoName" runat="server" ControlToValidate="txtCompanyName" Display="Dynamic" ErrorMessage="Please do not leave the Company Name blank." ValidationGroup="vgCompanyCreate"></asp:RequiredFieldValidator>
                </td>

c# C#

    protected void btnSubmitCompany_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            // Creating a new connection to the database. 
            -- Insert Connection String --

            // Creating a new command to insert the input values into the database.
            SqlCommand SubmitCompanyCmd = new SqlCommand("Insert into Employer ([EmployerID], [Name], [Status], [Remarks], [DateLastModified], [LastModifiedBy]) Values(@inputCoID, @inputCoName, @inputCoStatus, @inputCoRemarks, @currentDateTime, 'Admin')", conn);

            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoID", txtCompanyID.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoName", txtCompanyName.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoStatus", ddlCompanyStatus.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoRemarks", txtCompanyRemarks.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@currentDateTime", DateTime.Now);

            conn.Open();
            SubmitCompanyCmd.ExecuteNonQuery();
            conn.Close();
            lblSuccessMsg.Text = "Yay. xqc";
        }

edit: Ignore the "Insert connection string" part, the actual connection string works just fine as tested with other functions. 编辑:忽略“插入连接字符串”部分,实际的连接字符串在与其他功能一起测试时效果很好。

I managed to find the solution. 我设法找到了解决方案。

Basically what I found out is that all relevant fields need the validation group as well. 基本上,我发现所有相关字段也需要验证组。 Not just the validators. 不只是验证者。

The best way is to add code to your code behind (.cs class) 最好的方法是在(.cs类)后面的代码中添加代码

 protected void btnSubmitCompany_Click(object sender, EventArgs e)
    {
txtCompanyName.BorderColor = System.Drawing.Color.White;

if (txtCompanyName.Text="")
{
txtCompanyName.BorderColor = System.Drawing.Color.Red;
//Add error message if you want
}
else
{
if (Page.IsValid)
        {
            // Creating a new connection to the database. 
            -- Insert Connection String --

            // Creating a new command to insert the input values into the database.
            SqlCommand SubmitCompanyCmd = new SqlCommand("Insert into Employer ([EmployerID], [Name], [Status], [Remarks], [DateLastModified], [LastModifiedBy]) Values(@inputCoID, @inputCoName, @inputCoStatus, @inputCoRemarks, @currentDateTime, 'Admin')", conn);

            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoID", txtCompanyID.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoName", txtCompanyName.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoStatus", ddlCompanyStatus.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoRemarks", txtCompanyRemarks.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@currentDateTime", DateTime.Now);

            conn.Open();
            SubmitCompanyCmd.ExecuteNonQuery();
            conn.Close();
            lblSuccessMsg.Text = "Yay. xqc";
}
}

Or you could use the bootstrap "required" attribute which is very basic. 或者,您可以使用引导程序的“ required”属性,该属性非常基本。

<asp:TextBox ID="txtCompanyName" runat="server" BorderColor="#A32020" style ="border-right: solid 1px #a32020;" OnTextChanged="txtCompanyName_TextChanged"  requrired="true"></asp:TextBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM