Custom validator fires but does not prevent postback
Asked Answered
G

8

8

I've seen a lot of questions about this already, but I'm stumped! Please help!

I have a customvalidator. It's firing but it's not preventing postback. Please help me in doing so! I can see that console.log registers before the post. But, it posts back anyway. How do I prevent the postback?

I've tried adding a control to validate, and validate empty text equal to true. I also tried adding e.preventdefault, which did not work :(

How can I prevent the postback?

    <script type="text/javascript">
//<![CDATA[
function validateWhyUnlikely(source, args) {
    console.log(1);
    args.isValid = false;
}
//]]>

<asp:TextBox ID="txtWhyUnlikely" runat="server" Rows="4" cols="20"
            CssClass="surveyTextArea" />
<asp:CustomValidator runat="server" ID="cfvWhyUnlikley" ErrorMessage="Please provide a reason since you rated an item as unlikely to provide."
        CssClass="surveyError surveySmallIndent" Display="Dynamic" 
        ClientValidationFunction="validateWhyUnlikely" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="smallSpecial" OnClick="btnSubmit_Click" />


jQuery(document).ready(function () {
    jQuery('#<%= btnSubmit.ClientID %>').click(function (e) {
        if (Page.IsValid == false) {
            console.log(false);
            e.preventDefault();
            return false;
        }
    });
});
Gumbo answered 17/11, 2011 at 16:4 Comment(0)
S
7

Everything looks ok althought I am not sure why you are attaching the Click function to your submit button. I would remove that and test it as it maybe be overriding the default behavior.

Also I think you need to capitalize the IsValid property:

args.IsValid = false;
Slight answered 17/11, 2011 at 16:24 Comment(1)
Thanks... I wish JavaScript compiled! No errors, didn't see this casing error @SlightGumbo
U
3

I too faced this issue, I was trying to add a custom validator to a dropdownlist which had a selectedIndexChange event attached to it. After i gave incorrect value for dropdown, i was able to se ethe error message i gave in Custom Validator but immediately after it Postback was happening.

However on adding this property CausesValidation="true" to the dropdownlist control resolved my issue.

Postback wasn't happening on incorrect value after adding this property to my dropdown.

Uptake answered 7/6, 2012 at 7:54 Comment(0)
S
3

If it helps other people, I had a Validation group that I forgot to add the button to.

Make sure to add the button, the textbox and the validator to the same validation group for the postback to be prevented.

Sonyasoo answered 19/5, 2016 at 7:33 Comment(2)
Solved my problem 2 minutes ago. Thanks for the helpful answer and taking the time to let us know too. +1Willful
Correct, the point is you don't need all this code for preventing postback. This is something the control already does. Just need to specify validation group.Jugulate
C
2

I experienced this problem as well.

What I did was, in the C# procedure that was called by the button, at the top I added

if (IsValid == false)
         return;

I could not stop it performing the postback so this seemed to me like the only solution.

Cima answered 24/5, 2013 at 11:19 Comment(0)
I
1

You are misssing ControlToValidate="txtWhyUnlikely"

Igenia answered 28/1, 2014 at 18:13 Comment(0)
U
0

Posting this as it might help someone that is getting the same weird behavior.

Initially I had the same issue as this post title. I checked all the suggestions here but my code seemed to be fine.

To fix this I replaced my cause validation control <asp:Button.. with a <button.. . Not sure why this is happening but happy it's working now.

hth

Unwish answered 29/5, 2013 at 8:40 Comment(0)
Z
0

<button tags are missing the correct javascript code to validate. <asp:Button does have the correct javascript rendered.

I've added this to any button tags:

btn.Attributes("onclick") = StringFmt("if(!Page_ClientValidate(''))return false;")

and that solved the post-back issue. No post-back occurs if the client-side detects an issue.

Zante answered 26/10, 2014 at 3:0 Comment(0)
B
0

I solved this problem by creating a variable:

 Boolean fieldIsValid = true;

and at the custom validating expression I would change the value if arguments weren't true:

if(args.IsValid == false)
            {
                fieldIsValid = false;
            }
            else
            {
                fieldIsValid = true;
            }

Then, I also put that in the submit click method:

protected void submit_Click(object sender, EventArgs e)
        {
            if (fieldIsValid)
            {
                //submit my things
            }
        }
Buiron answered 27/6, 2017 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.