views:

97

answers:

4

I have a .NET page that has a submit button. I am using client side validation to validate the form element.

var register = $('#<%= Register.ClientId %>');
register.click(function(){
    validation.init();
    return false;
});

Now, the return false on top prevents the form from being submitted to the server on click. In any other language, once I do the validation, I would then call the form submit method and well, it will work. Somehow, this doesn't seem to work in .NET. I am not sure if this is because the form tag itself is wrapped around the entire page content or not.

+2  A: 

You should use a ASP.NET CustomValidator to do this hook, this will be wired up to run on form.submit automatically.

Dated and old VB tutorial, but you can get the gist of it: http://www.4guysfromrolla.com/articles/073102-1.aspx

FlySwat
Any other way around it? I'm not so crazy about the extra spaces/html that appear with the custom validator.
Dhana
Set the CustomValidator to display='dynamic' and don't get it any error message. Then just handle your error show/hide in the customvalidator's JS function.
FlySwat
A: 

Instead of always returning false, perform the validation, and then return true or false based on the result.

David Dorward
I tried that, the form submits no matter what - so I had to force the return false earlier on.
Dhana
A: 

form.submit() works fine with ASP.NET - you have some other issue. Post more code and we'll see what it is....

<script runat="server">
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        if (IsPostBack) {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('PostBack!');", true);
        }      
    }
</script>

<body>
    <script type="text/javascript">
        function OnClick() {
            window.setTimeout(function() { form1.submit(); }, 5000);
            return false;
        }
    </script>
    <form id="form1" runat="server">
        <input type="submit" onclick="return OnClick();" />      
    </form>
</body>
Mark Brackett
A: 

I do this kind of thing all the time. Especially if I have some complicated validation that I don't want to screw around with the validator controls. document.forms[0].submit();

That works every time for me. Is this not working for you?

Matt Dawdy