有条件地禁用验证

发布于 2024-11-18 08:31:54 字数 1540 浏览 4 评论 0原文

我的表单带有几个经过验证的文本框(服务器端和客户端)。在表单中,我有按钮:“下一步”、“后退”、“取消L”。因此,我不需要验证即可启动,然后用户单击“后退”或“取消”按钮。我怎样才能实现这个目标? 提前致谢!


一些示例:

<div class="buttons">    
<input type="submit" name="cancelButton" value="" />
<input type="submit" name="backButton" value="" />
<input type="submit" name="nextButton" value="" />
</div>

<% using (Html.BeginForm()) { %>
    <p>

    <table style="width: 200px">    
    <tr><td align="center" colspan=2><%= Html.ValidationMessageFor(m => m.street) %><%= Html.DropDownListFor(m => m.street, Model.streetsList) %></td></tr>
    <tr><td colspan=2>&nbsp;</td></tr>
    <tr><td valign="bottom" align="right" style="width: 75px"><%= Html.LabelFor(m => m.flatNumber) %>:</td><td align=left><%= Html.TextBoxFor(m => m.flatNumber, new { maxlength = 6, style = "width: 48px;" })%> <%= Html.ValidationMessageFor(m => m.flatNumber) %></td></tr>
    </table>
    <br />

        <input type="submit" class="refusal button red floatL" name="cancelButton" value="" />
        <input type="submit" class="back button green floatL" name="backButton" value="" />
        <input type="submit" class="continue button green floatR marR" name="nextButton" value="" />
    </div>
    <div class="clear">
    </div>
    <% } %>

在服务器端,我使用 DataAnnotations 属性进行验证。

I have form with few text boxes which goes through validation (both server and client sides). In the form I have buttons: "Next", "Back", "CanceL". So I don't need validation to fireup then user clicks "back" or "cancel" buttons. How can I achieve this?
Thanks in advance!


Some sample:

<div class="buttons">    
<input type="submit" name="cancelButton" value="" />
<input type="submit" name="backButton" value="" />
<input type="submit" name="nextButton" value="" />
</div>

<% using (Html.BeginForm()) { %>
    <p>

    <table style="width: 200px">    
    <tr><td align="center" colspan=2><%= Html.ValidationMessageFor(m => m.street) %><%= Html.DropDownListFor(m => m.street, Model.streetsList) %></td></tr>
    <tr><td colspan=2> </td></tr>
    <tr><td valign="bottom" align="right" style="width: 75px"><%= Html.LabelFor(m => m.flatNumber) %>:</td><td align=left><%= Html.TextBoxFor(m => m.flatNumber, new { maxlength = 6, style = "width: 48px;" })%> <%= Html.ValidationMessageFor(m => m.flatNumber) %></td></tr>
    </table>
    <br />

        <input type="submit" class="refusal button red floatL" name="cancelButton" value="" />
        <input type="submit" class="back button green floatL" name="backButton" value="" />
        <input type="submit" class="continue button green floatR marR" name="nextButton" value="" />
    </div>
    <div class="clear">
    </div>
    <% } %>

At the server side I use DataAnnotations attributes for validation.

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

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

发布评论

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

评论(2

青巷忧颜 2024-11-25 08:31:54

Button 类有一个 CausesValidation 属性 - 如果将其设置为 false,则回发时不会触发验证。

示例:

<asp:Button id="btnCancel" CausesValidation="false" onClick="bntCancel_Click" Text="Cancel" runat="server" />

请注意,这将禁用 ASP.NET 验证器 - 如果您有自己的验证器,则需要以其他方式禁用它。

the Button class has a CausesValidation property - if that is set to false, validation won't be triggered on postback.

Example:

<asp:Button id="btnCancel" CausesValidation="false" onClick="bntCancel_Click" Text="Cancel" runat="server" />

Note that this will disable the ASP.NET validators - if you have your own validation you will need to disable it another way.

浮世清欢 2024-11-25 08:31:54

用表单包围文本框,然后将“下一步”、“后退”和“取消”变成提交按钮。在事件 onsubmit 上,分配一个方法,如果表单有效并且应继续将其发送到服务器,则该方法返回 true,否则返回 false。

因此,我希望得到以下内容:

<form id="navigatorForm">
    <!-- Various text forms here -->

    <input type="submit" value="Back" onsubmit="back()" />
    <input type="submit" value="Next" onsubmit="next()" />
    <input type="submit" value="Cancel" onsubmit="cancel()" />
    <input type="hidden" id="operation" name="operation" value="" />
</form>

<script type="text/javascript">
function validate() {
    // Perform validation here
    // If okay, return true, else false
}

function next() {
    document.getElementById('operation').value = 'next';
    if(!validate()) {
        alert('Form is not filed correctly.  Please pay more attention!');
        return false;  // Do not send to server!  
    } else {
        return true;
    }
}

function back() {
    document.getElementById('operation').value = 'back';
    return true;
}

function cancel() {
    document.getElementById('operation').value = 'cancel';
    return true;
}
</script>

请注意,与 next() 不同,back() 和 cancel() 无条件返回 true,这意味着请求在任何情况下都会发送到服务器。此时,在服务器端,您只需检查下一步是否有操作即可知道是否应该执行进一步的测试。

Surround the text boxes with a form and turn next, back, and cancel into submit buttons. On event onsubmit, assign a method which returns true if the form is valid and should proceed to send it to the server, otherwise false.

So I would expect something along the lines of:

<form id="navigatorForm">
    <!-- Various text forms here -->

    <input type="submit" value="Back" onsubmit="back()" />
    <input type="submit" value="Next" onsubmit="next()" />
    <input type="submit" value="Cancel" onsubmit="cancel()" />
    <input type="hidden" id="operation" name="operation" value="" />
</form>

<script type="text/javascript">
function validate() {
    // Perform validation here
    // If okay, return true, else false
}

function next() {
    document.getElementById('operation').value = 'next';
    if(!validate()) {
        alert('Form is not filed correctly.  Please pay more attention!');
        return false;  // Do not send to server!  
    } else {
        return true;
    }
}

function back() {
    document.getElementById('operation').value = 'back';
    return true;
}

function cancel() {
    document.getElementById('operation').value = 'cancel';
    return true;
}
</script>

Notice that unlike next(), back() and cancel() unconditionally return true, meaning that the request is sent to the server in any circumstance. At this point, on the server side, you'd need only to check to see if operation is next to know whether or not you should perform further testing.

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