0

I would like to put a condition on a jQuery get request depending on the data I get.

Here is my function:

function validateForm() {
    var username = $('#username').val()
    var result = ''

    $.get(
        '/auth_validate_username/', 
        { 'result': result }, 
        function(data) {
            $.post(
                '/auth_validate_username/', 
                { 'username': username }, 
                function(data) { 
                    $('.error_message').html(data); 
                }
            );
        }
    );
    return false;
};

HTML:

<form  method="post" onsubmit="return validateForm();">
    <input id='username' type="text" name="username"/>
    <div class='error_message'></div>
    <button type="submit">Valider</button>
</form> 

I would like to put a condition so that if the result I get with my get function is not empty, then the form is not submitted. But if result != '', then I want the form to be submitted.

Any idea on how I could put a condition on the form inside my get request?

PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
Marcolac
  • 901
  • 4
  • 14
  • 27
  • you need to use form validator plugin or create custom there itself. – Rafee Dec 20 '12 at 09:10
  • Form validator? How about an if statement? By the way, your quotes around `'result'` and `'username'` as object keys are not necessary. – Aesthete Dec 20 '12 at 09:16

1 Answers1

3

In the function executed after the get request, you can execute any javascript statement. Including if.

function validateForm() {
    var username = $('#username').val()
    var result=''

    $.get(
        '/auth_validate_username/', 
        { 'result': result }, 
        function(data) {

            // Magical line there
            if (data !== '') {

                $.post(
                    '/auth_validate_username/', 
                    { 'username': username }, 
                    function(data) { 
                        $('.error_message').html(data);
                    }
                );
            }
        }
    );
    return false;
};
Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • Thank you. The condition I would like to add is: if(data!==''){return false;}. The problem is that I would like the return false; to be applied to the function validateForm() so that my form is not submitted. It looks like it is not the case when I try it. – Marcolac Dec 20 '12 at 09:34
  • @Marcolac then you can play with `e.preventDefault()`. Pass `e` in your `validateForm` function and use `e.preventDefault()`. – Florian Margaine Dec 20 '12 at 09:44
  • Ok thank you. I don't know a thing about e.preventDefault(). I'll check how to use it. Thank you for your help. – Marcolac Dec 20 '12 at 09:50
  • I checked e.preventDefault() and I really don't see how it could do the trick. Do I have to put it inside the condition in my get request? – Marcolac Dec 20 '12 at 10:04
  • do something like `function validateForm(e) { /* ...... */ if (data !== '') { e.preventDefault(); }` – Florian Margaine Dec 20 '12 at 10:05
  • Yes, that's what I tried, but it doesn't work. The form is submitted. – Marcolac Dec 20 '12 at 10:08