开发者

firing both onclientclick and Client side validation from validation control on button Client click

I have few ASP Text Box controls on a Page, to which Custom Validators are added. I have Save Button, which validates these Text boxes. I have just added the Validation Group as same as that of the Text boxes.

In order to incorporate addtional requirement, I have added onClientClick function for Save button. But amused to find that Validation due to Validation Group(validator controls) is not firing instead the onClientClick function is called and server side Click event is called.

Javascript Code

function ValidateInputs(source, args) {
            var regex = /^(-{0,1}\d{1,100})$/;
            if (args.Value.length < 1) {
                $("span[id$='spn_error']").html('Please Enter ' + $(source).attr("errormessage"));
                $("span[id$='spn_error']").show();
                args.IsValid = false;
                return;
            }
            else if (!regex.test(args.Value)) {
                $("span[id$='spn_error']").html($(source).attr("errormessage") + ' allows only numbers');
                $("span[id$='spn_error']").show();
                args.IsValid = false;
                return;
            }
            else {
                if ($("span[id$='spn_error']").html().indexOf($(source).attr("errormessage")) >= 0)
                    $("span[id$='spn_error']").hide();
                args.IsValid = true;
                return;
            }
        }

function isValidTracker() {
//Dummy Code Say Confirm button
return(confirm('Are you sure');)

}

HTML Code

<span class="errormesg" runat="server" id="spn_error" style="display: none;
                        font-size: 9px;"></span>
<asp:TextBox ID="txtLastMonthCount" runat="server" CssClass="inputbox" Width="75px" autocomplete="off" onkeypress="return isNumberKey(event);" ValidationGroup="g1"></asp:TextBox>

<asp:CustomValidator ID="CVLastMonthCount" runat="server" ErrorMessage="Last Months Closing Count" Text="<img src='../images/alert.gif' alt='Please Enter Last Months Closing Count' title='Please Enter Last Months Closing Count' />" 
    ValidationGroup="g1" ClientValidationFunction="ValidateInputs" OnServerValidate="ValidateInputs" ControlToValidate="txtLastMonthCount" ValidateEmptyText="true开发者_C百科"></asp:CustomValidator>

<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="button" ValidationGroup="g1" OnClientClick="return isValidTracker();" OnClick="btnSave_Click" />

Please help me in finding the solution to fire onclientclick and validation control of Save button click.


Change OnClientClick from return isValidTracker() to if (!isValidTracker()) return false;


Thanks for the Solutions provided. I too got a solution while googling, thought of sharing it, as it would be helpful in various other scenarios for others like me.

function isValidTracker() {

    if (Page_IsValid) {
        return(confirm('Are you sure');)//Dummy Code Say Confirm button
    }
    else
    {
        return false;
    }

    }

Where Page_IsValid is the Java script Variable set by the ASP.Net Validation Control. It is set to true when all the validation controls are successfully validated. Thanks :)


OneHalf's solution works, but I'd bake the final check into your validator - this ensures you're not asking the user "are you sure?" until you know their action is valid...

    function ValidateInputs(source, args) {
    var regex = /^(-{0,1}\d{1,100})$/;
    if (args.Value.length < 1) {                 
        $("span[id$='spn_error']").html('Please Enter ' + $(source).attr("errormessage"));
        $("span[id$='spn_error']").show();                 
        args.IsValid = false;                 
    }             
    else if (!regex.test(args.Value)) {                 
        $("span[id$='spn_error']").html($(source).attr("errormessage") + ' allows only numbers');                
        $("span[id$='spn_error']").show();                 
        args.IsValid = false;                
    } else {                 
        if ($("span[id$='spn_error']").html().indexOf($(source).attr("errormessage")) >= 0)                     
            $("span[id$='spn_error']").hide();                 
        args.IsValid = true;
    }

    if (args.IsValid) args.IsValid = isValidTracker();
}  

Though I would agree that there's been an oversight on the ASP.NET team's part - your implementation seems more intuitive and should have been supported.

B


Hello I have also face the same issue. CausesValidation="true" property solved my problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜