在使用jQuery的ajax上需要帮助[重复]

This question already has answers here:
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2013-01-24 09:32:30Z" class="relativetime">7 years ago</span>.</div>
        </div>
    </aside>

Possible Duplicate:
How to return the response from an AJAX call from a function?

i am creating a registration page with email id and password . i have called onsubmit event in registration page which call's checkData() function which is written in a common.js file. i have a ajax function to check whether email id exist in database or not ? but my problem is when i keep alert message after the completion of checking my ajax function works fine but if alert is removed it enter's data in to database. here is the code of ajax

   else
   {
       $(function() {

              $.ajax({
                    url  : 'RegCheck.php',
                    type : 'POST',
                    data : 'User=' + address,
                    success : function(result){
                       if(result != "No")
                       {
                           document.getElementById("EmailChk").innerHTML = result;    
                           Valid = false;

                       }
                    }

              });              


       });                         
      document.getElementById("MailMand").innerHTML = "" ;

   }    
   alert(Valid);
   return Valid;
</div>

ajax doesn't work that way. The success code will execute after the return Valid code 99.9% of the time (if not 100% of the time). You can't guarantee when the success will be called.

Instead, you need to do all of your work in the ajax callback or (shudder) make it synchronous.

So if you're depending on it for validation, you need to stop the form from submitting and then submit the form in the success method.