ASP验证组不起作用

时间:2015-10-29 09:05:39

标签: asp.net validation

有几个页面显示相同的代码,只是不同的字段名称。

我努力理解的是,为什么验证不会在按钮点击上调用,因为按钮点击设置为导致验证,所有字段,必填字段验证器和正则表达式验证器都具有相同的组。

更新  我记得以前这个工作(一个被丢弃然后被拾起的项目),所以我能想到的唯一的事情是web.config或c#后端停止它。我不会想到页面的后端正在执行此操作,因为按钮会导致点击验证。

还有什么可以阻止这个?

代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/DirectDebit/MasterDDSite.Master" AutoEventWireup="true" CodeBehind="DDI_2.aspx.cs" Inherits="CustomerServicePortal.DDI_Forms.DDI_2" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        function CheckNumeric(e) {

            if (window.event) // IE 
            {
                if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
                    event.returnValue = false;
                    return false;
                }
            }
            else { // Fire Fox
                if ((e.which < 48 || e.which > 57) & e.which != 8) {
                    e.preventDefault();
                    return false;

                }
            }
        }
    </script>
    <style type="text/css">
        .style1 {
            height: 28px;
        }

        table {
            border-spacing: 2px;
            border-collapse: separate;
        }

        .valError {
            color: red;
            border: 2px solid red;
            margin: 5px 0px;
            padding: 15px;
        }

            .valError ul {
                color: red;
            }

            .valError li {
                color: red;
            }

        .validation_summary {
            color: red !important;
        }
    </style>
    <div class="jumboservice">
        <h2>Company Details</h2>
        <div id="cmpnyDetails">
            <div class="ui-grid-a">
                <div class="ui-block-a">
                    <h4>Title:</h4>
                </div>
                <div class="ui-block-b">
                    <span>
                        <asp:TextBox ID="title" runat="server" ValidationGroup="vg1"></asp:TextBox>
                        *</span>
                    <asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="title" ValidationGroup="vg1" ErrorMessage="Title is a required field." Display="None"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator Display="None" ValidationGroup="vg1" ControlToValidate="title" ID="Rev3" ValidationExpression="^[\s\S]{2,5}$" runat="server" ErrorMessage="Title requires between 2 and 5 characters."></asp:RegularExpressionValidator>
                </div>
            </div>
            <div class="ui-grid-a">
                <div class="ui-block-a">
                    <h4>Forename(s):</h4>
                </div>
                <div class="ui-block-b">
                    <span>
                        <asp:TextBox ID="forename" runat="server" ValidationGroup="vg1"></asp:TextBox>
                        *</span>
                    <asp:RegularExpressionValidator Display="None" ValidationGroup="vg1" ControlToValidate="forename" ID="RegularExpressionValidator1" ValidationExpression="^[\s\S]{2,25}$" runat="server" ErrorMessage="Forename requires between 2 and 25 characters."></asp:RegularExpressionValidator>
                    <asp:RequiredFieldValidator ID="rfv2" runat="server" ControlToValidate="forename" ValidationGroup="vg1" ErrorMessage="Forename is a required field." Display="None"></asp:RequiredFieldValidator>
                </div>
            </div>
            <div class="ui-grid-a">
                <div class="ui-block-a">
                    <h4>Surname:</h4>
                </div>
                <div class="ui-block-b">
                    <span>
                        <asp:TextBox ID="surname" runat="server" ValidationGroup="vg1"></asp:TextBox>
                        *</span>
                    <asp:RequiredFieldValidator ID="rfv3" runat="server" ControlToValidate="surname" ValidationGroup="vg1" ErrorMessage="Surname is a required field." Display="None"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator Display="None" ValidationGroup="vg1" ControlToValidate="surname" ID="RegularExpressionValidator2" ValidationExpression="^[\s\S]{3,45}$" runat="server" ErrorMessage="Surname requires between 3 and 45 characters."></asp:RegularExpressionValidator>
                </div>
            </div>
            <div class="ui-grid-a">
                <div class="ui-block-a">
                    <h4>Telephone:</h4>
                </div>
                <div class="ui-block-b">
                    <span>
                        <asp:TextBox ID="CompanyTele" runat="server" onkeypress="CheckNumeric(event);" ValidationGroup="vg1"></asp:TextBox>
                        *</span>
                    <asp:RegularExpressionValidator Display="None" ValidationGroup="vg1" ControlToValidate="CompanyTele" ID="RegularExpressionValidator10" ValidationExpression="^[0-9]{10,20}$" runat="server" ErrorMessage="Telephone requires between 10 and 20 numbers."></asp:RegularExpressionValidator>
                    <asp:RequiredFieldValidator ID="rfv9" runat="server" ControlToValidate="CompanyTele" ValidationGroup="vg1" ErrorMessage="Telephone is a required field." Display="None"></asp:RequiredFieldValidator>
                </div>
            </div>
        </div>
        <p><strong>*</strong> - Fields marked with an asterisk are mandatory.</p>
        <asp:ValidationSummary ID="ValidationSummary1"
            runat="server" ShowMessageBox="false"
            ShowSummary="true" ValidationGroup="vg1"
            ForeColor="Red" DisplayMode="BulletList"
            CssClass="valError" HeaderText="<span><strong>The below requires attention:</strong></span>" />
        <div class="ui-grid-d">
            <div class="ui-block-a">
                <div data-role="main" class="ui-content">
                    <div data-role="controlgroup" data-type="horizontal">
                        <asp:Button ID="Submit" CausesValidation="true" ValidationGroup="vg1" runat="server" CssClass="ui-btn ui-corner-all" Text="continue" OnClick="Submit_Click" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</asp:Content>

1 个答案:

答案 0 :(得分:4)

删除&#34;验证组&#34;从文本框字段。您应该只添加&#34;验证组&#34;在验证字段中,引用您要验证的控件。