views:

32

answers:

3

Hey guys,

I'm working on a jQuery function that forwards form data to page without interfering with the normal submission. I can do it without any issues as long as I capture the submit using .submit(), but I would have to run my own validation on the data because it operates independently of the regular submission. Is there a way for jQuery (or any Javascript) to detect that form data has been posted and validated?

cheers, Mike

Edit:

Workflow looks like this:
1. User enters data
2. Clicks submit
3. Site runs validation and accepts input
4. Submits data to new page
5. jQuery function detects new data was submitted and accepted so it runs.

More Edits for Clarity

I think you guys are missing the issue. I know how to detect a form is being submited (which is fine and dandy)

This is NOT what I want:

    $(this).each(function(){
           $(this).submit(function(){
                *** Code ***
           }
       }

Suppose I have a validation script running independent of the code I am currently writing. How can I detect that this ran, and then go to the submit code above?

+2  A: 

Use onsubmit="" on your <form> element, but return false. i.e.:

<form action="?" method="post" onsubmit="validate_and_submit(this);return false;">

The return false prevents the form from actually submitting so you can do stuff with AJAX.

Hope this helps!

mattbasta
The problem is I want the form to submit so that the validation can run. Workflow looks like this: 1. User enters data 2. Clicks submit 3. Site runs validation and accepts input 4. Submits data to new page 5. jQuery function detects new data was submitted and accepted so it runs. Any thoughts?
Mike
I'm not sure I understand steps 3-5. You're sending the data to the server with AJAX, validating it, then having the client submit the data for real?
mattbasta
+1  A: 

EDIT: for the original clarification

If you want the server to only received validated data, then just make sure its not submitted to prior to the client-side validation occuring. You can do this with selectively calling event.preventDefault() based on the result of the validation.

$("#form").submit(function(event) {

     //some stuff
     if (validate(formdata) == false) { 
        event.preventDefault();
     } 

});

If you want a server to do the validation and submit to itself or another service, you should make that part of the server-side workflow. So it'd be like

1.) client submits to Service1

2.) Service1 validates

3.) Services1 submits to Service2 (such that service2 never receives code from elsewhere)

Taylor
What if I don't know what the name of the validation function is? Can I have the user store it in a variable and then call it through the variable?
Mike
eval() should work. -- if (eval(callStr) == false) -- Build up callStr as functionNameStr + ( + params + ) whenever you have the name
Taylor
+2  A: 

What you need is AJAX here . So make a XHR request that goes to your server and posts data . The server's response would now go to a callback function ( your jquery function ) . If the data was validated and fine , you proceed further , else you stop .
What you are trying to do is not possible via the normal HTTP POST request .

NM
But is it possible to allow the current page to run its own validation rather than having to validate the AJAX request myself?
Mike
@Mike Build validation into the page accepting the data. Don't have it as a separate page, aside from a validation library, that is included not posted to directly.
George Marian
The issue with that is that it's designed to act independently of the original page, so I won't necessarily know what sort of data I'm getting.
Mike
@Mike What's stopping you from re-designing things? (Or, more correctly, re-architecting.)
George Marian
@Mike - You want to validate on the server or on the client ? If the data needs to get validated on the server , then I dont think the "page" itself can do it .
NM