自定义验证器

发布于 2024-11-26 22:14:16 字数 1170 浏览 1 评论 0原文

所以我有一个只能部分工作的自定义验证器。它基本上检查两件事:是否填写了两个字段,以及这些字段中输入的内容是否已存在于数据库中。检查数据库工作正常,但检查字段是否填写则不然。我不想使用必填字段验证器,因为我希望错误消息全部位于页面上完全相同的位置。我很确定我只是搞砸了一些简单的事情,但我就是找不到它。

<strong>Course Prefix and Number:</strong>
<asp:TextBox ID="txtCoursePrefix" runat="server" Width="45" MaxLength="4" CssClass="caps"></asp:TextBox>
-
<asp:TextBox ID="txtCourseNum" runat="server" Width="45" MaxLength="6" CssClass="caps"></asp:TextBox>
<span class="required">*
<asp:CustomValidator ID="cvDuplicate" runat="server" ControlToValidate="txtCoursePrefix" ValidateEmptyText="true"></asp:CustomValidator>
</span>

隐藏代码:

'Check if fields have been filled out
    If txtCoursePrefix.Text Is Nothing Or txtCourseNum.Text Is Nothing Then
        cvDuplicate.ErrorMessage = "Required"
        args.IsValid = False
    End If
'Code that checks values against database goes here
'If matching record does not exist...
    If myValue IsNot Nothing Then
        cvDuplicate.ErrorMessage = "Course number is already taken."
        args.IsValid = False
    End If

所以,第一部分再次不起作用,第二部分工作正常。

So I have a custom validator that is working only partially. It basically has two things it's checking for: if two fields are filled out, and whether or not what has been entered in those fields already exists in the database. Checking against the database is working fine, but checking whether or not the fields are filled out is not. I don't want to use required field validators, since I want the error messages all in the exact same location on the page. I'm pretty sure I just messed up on something simple, but I just can't find it.

<strong>Course Prefix and Number:</strong>
<asp:TextBox ID="txtCoursePrefix" runat="server" Width="45" MaxLength="4" CssClass="caps"></asp:TextBox>
-
<asp:TextBox ID="txtCourseNum" runat="server" Width="45" MaxLength="6" CssClass="caps"></asp:TextBox>
<span class="required">*
<asp:CustomValidator ID="cvDuplicate" runat="server" ControlToValidate="txtCoursePrefix" ValidateEmptyText="true"></asp:CustomValidator>
</span>

Code behind:

'Check if fields have been filled out
    If txtCoursePrefix.Text Is Nothing Or txtCourseNum.Text Is Nothing Then
        cvDuplicate.ErrorMessage = "Required"
        args.IsValid = False
    End If
'Code that checks values against database goes here
'If matching record does not exist...
    If myValue IsNot Nothing Then
        cvDuplicate.ErrorMessage = "Course number is already taken."
        args.IsValid = False
    End If

So once again it is the first part that's not working, the second part is working fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浸婚纱 2024-12-03 22:14:16

文本框可能被视为空字符串,而不是空字符串。尝试一下这个检查:

If String.IsNullOrEmpty(txtCoursePrefix.Text) Or String.IsNullOrEmpty(txtCourseNum.Text) Then
    cvDuplicate.ErrorMessage = "Required"
    args.IsValid = False
End If

It might be that the Text box is considered an empty string, not Nothing. Try this for your check:

If String.IsNullOrEmpty(txtCoursePrefix.Text) Or String.IsNullOrEmpty(txtCourseNum.Text) Then
    cvDuplicate.ErrorMessage = "Required"
    args.IsValid = False
End If
书信已泛黄 2024-12-03 22:14:16
'Check if fields have been filled out
If String.IsNullOrEmpty(txtCoursePrefix.Text.Trim()) _
        OrElse String.IsNullOrEmpty(txtCourseNum.Text.Trim()) Then
    cvDuplicate.ErrorMessage = "Required"
    args.IsValid = False
End If
'Check if fields have been filled out
If String.IsNullOrEmpty(txtCoursePrefix.Text.Trim()) _
        OrElse String.IsNullOrEmpty(txtCourseNum.Text.Trim()) Then
    cvDuplicate.ErrorMessage = "Required"
    args.IsValid = False
End If
私藏温柔 2024-12-03 22:14:16

需要检查 2 件事

  1. 添加以下FieldRequired="True"

  2. 开服务器端检查是否设置了以下任何位置
    this.cvDuplicate.Enabled = false;

如果将以下属性设置为 false。即使您从服务器端的数据库进行设置,也不会进行验证。所以设置这些时要小心。如果您没有在 .ASCX 上设置这些属性并尝试从服务器端的数据库(C# 代码)设置它们,那么它们将起作用。
this.cvDuplicate.Enabled = true;

ValidateEmptyText="False"
FieldRequired="False"

2 things needs to be checked

  1. Add following FieldRequired="True"
    <asp:CustomValidator ID="cvDuplicate" runat="server" ControlToValidate="txtCoursePrefix" ValidateEmptyText="true" FieldRequired="True"></asp:CustomValidator>

  2. On Server Side check if any place following is setup
    this.cvDuplicate.Enabled = false;

If you set following property as false. No validation will occur even if you set it up from Database on server side. So be careful when you set these up. If you do not set up these properties on .ASCX and set try to set them up from Database on server side (C# code) then they will work.
this.cvDuplicate.Enabled = true;

ValidateEmptyText="False"
FieldRequired="False"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文