简体   繁体   中英

Ajax call after successfull jQuery validation issue

Using RSV jQuery Validation from here .

Really stuck at one place. I wrote custom error handler for this plug-in (read documentation, it's allowed), now it shows errors, and focuses at exact fields one by one, then at the end of validation my custom handler return (errorInfo.length == 0) ? true : false; return (errorInfo.length == 0) ? true : false; returns true if there is no error. The problem is, after this rsv directly send form data to PHP. But I want to fire Ajax function after successfull validaton. I wrote another function wellDone() for "on complete" event seems plugin doesn't fire oncomplete function at all. Please help me to fix that problem.

$(document).ready(function() {
    $("#signup_form").RSV({
        onCompleteHandler: wellDone,
        customErrorHandler: errorHandler,
        rules: signup_rules
    });
}); 
function errorHandler(f, errorInfo)
{
    for (var i=0; i<errorInfo.length; i++)
    {
        // errorInfo[i][0] contains the form field node that just failed the validation, e.g.
        errorInfo[i][0].focus();
        // errorInfo[i][1] contains the error string to display for this failed field, e.g.
        $.notifyBar({
            cls: "error",
            html: errorInfo[i][1]
        });

    }

return (errorInfo.length == 0) ? true : false;
}

function wellDone(){
    signUp();
}

var signup_rules = [
<some validation rules>...
]

If you use a customErrorHandler , then in fact the onCompleteHandler will never be called. It is assumed you call it yourself at the end of your customErrorHandler when errorInfo.length == 0 .

可能您应该将从customErrorHandler函数返回的true或false值缓存在一个变量中(所有执行ajax调用的函数都可以使用该变量),并使用它来确定是否触发ajax调用。

Your customErrorHanlder should always return "false", but not " balabala ? true : false"

This is for people that are comfortable with javascript and who need a little more control over how the errors are presented. It lets you define your own custom function that gets passed the list of errors that occur on a form submit. Here's a simple example that alerts each error in turn. Note: the two function parameters MUST be set in order to be passed the error information properly.

customErrorHandler:

$(document).ready(function() {
  $("#demo_form1").RSV({
    customErrorHandler: myReturnFunction,
    rules: [
      // ...
    ]
  });   });
 /**    
  * @param f the form node    
  * @param errorInfo an array of arrays. Each sub-array has two elements: the field node and the error message.    
  */   
function myReturnFunction(f, errorInfo)   {
  for (var i=0; i<errorInfo.length; i++)
  {
    // errorInfo[i][0] contains the form field node that just failed the validation, e.g.
    errorInfo[i][0].focus();
    errorInfo[i][0].style.color = "red";

    // errorInfo[i][1] contains the error string to display for this failed field, e.g.
   alert(errorInfo[i][1]);
  }

  return false; // always return false! Otherwise the form will be submitted**   
}

see the last line of code and its comment? ALWAYS return false :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM