与JavaScript一起工作的RequiredFieldValidator(Working Req

2019-06-26 09:22发布

我有多个RequiredFieldValidator的,如:

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtbox1" 
            Display="Dynamic" ErrorMessage="Required Field" SetFocusOnError="True" 
            ValidationGroup="validator1" CssClass="validator" />

与此相关的按钮:

<asp:LinkButton runat="server" ID="btnNext1" Text="Next Page" CssClass="btn" ValidationGroup="validator1" />

随着一些JavaScript:

<script type="text/javascript">
$(function() {
    function nextPage1() {

        $( "#divFirstPage" ).hide("fade");
        $( "#divSecondPage" ).show("fade");
        $( "#<%=btnNext1.ClientID%>" ).hide();
        $( "#<%=btnNext2.ClientID%>" ).show();
        $( "#<%=btnPrevious1.ClientID%>" ).show();
    };
    $( "#<%=btnNext1.ClientID%>" ).click(function() {
        nextPage1();
        return false;
    });
    $( "#divSecondPage" ).hide();
    $( "#divThirdPage" ).hide();
    $( "#<%=btnNext2.ClientID%>" ).hide();
    $( "#<%=btnPrevious1.ClientID%>" ).hide();
    $( "#<%=btnPrevious2.ClientID%>" ).hide();
});
</script>

但JavaScript的得到验证之前执行的,所以ID需要的JavaScript 之前执行的验证

Answer 1:

如果我的理解是正确的,你要执行JavaScript代码之前验证表单

尝试是这样的:

$( "#<%=btnNext1.ClientID%>" ).click(function() {
    var val = Page_ClientValidate();
    if(!val) {
        return false;
    }
    nextPage1();
    return true;
});

可选如果要指定一个自定义ValidationGroup ,您可以使用下面的代码:

Page_ClientValidate('your group name');


文章来源: Working RequiredFieldValidator along with javascript