在必填字段上进行验证

时间:2014-10-06 12:20:49

标签: javascript html asp.net requiredfieldvalidator

从验证示例开始: http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_validationsum

使用asp.net我想显示错误消息'您必须在以下字段中输入值:'以包含错误输入的字段名称。

到目前为止,用户无法继续,直到输入正确的数据并且列旁边出现红色*。我现在想添加错误消息。

 <div id="floater_EditData_Panel_popup" title="Feature Information" class="floaterDiv">
Select a feature to view/edit feature information.
<p>&nbsp;</p>
  <asp:UpdatePanel runat="server" id="UpdatePanelDetails1" updatemode="Conditional" Childrenastriggers="false">
    <ContentTemplate>
      <asp:PlaceHolder ID="PlaceHolder1" runat="server" >
      </asp:PlaceHolder>
      <br />
      <br />
      <div id="EditDataPanelMessageOutput" style="color:Red;">

          <asp:ValidationSummary ID="ValidationSummary1"
        HeaderText="You must enter a value in the following fields:"
        DisplayMode="BulletList"
        EnableClientScript="true"
        runat="server"/>

      </div>
      <div id="FeatureMeasureOutput"></div>
      <br />
      <br />
      <div class="buttonwrap">
        <div id="Span2" class="ActionBtns" style="display: inline-block;" >
          <asp:button runat="server" id="UpdateButton" value="Save" Text="Save" OnClientClick="ValidateEditData();" CausesValidation="true" ValidationGroup="g_currentSelectedLayerName" />

        </div>
      </div>
    </ContentTemplate>
  </asp:UpdatePanel>
</div>


    function ValidateEditData() {
          if (Page_ClientValidate(g_currentSelectedLayerName)) {
              //alert('it is valid');
              updateFeature();
              return true;
          }
          else {
              //alert('Data not valid');
              return false;
          }
      }

验证摘要标记不适用于整个页面吗?

1 个答案:

答案 0 :(得分:0)

怎么样:

    <asp:ValidationSummary ID="ValidationSummary1"
    HeaderText="You must enter a value in the following fields:"
    DisplayMode="BulletList"
    EnableClientScript="true"
    ValidationGroup="g_currentSelectedLayerName"
    runat="server"/>

    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>

    <asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="txtFirstName" ErrorMessage="First Name is required" ValidationGroup="g_currentSelectedLayerName"></asp:RequiredFieldValidator>

    <asp:Button runat="server" Text="Submit" ValidationGroup="g_currentSelectedLayerName" />

然后在您的web.config中添加appSetting:

<appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>