简体   繁体   中英

mootools | Unique email verification

I have a mootools script:

Form.Validator.add('UniqueEmail', {
   errorMsg: 'Email is taken',
   test: function(element, props) {

      var valid = false;
      if (element.value.length > 0) {

         site.request(app, {'action':'account.email', 'email':element.value}, function(r){ 
           valid = parseInt(r.istaken) != 1 ? true : false;             
         },'GET');

         return valid;
      }
   }
});

/ Server returns: {"status":1,"istaken":0} / But input field verification : failed. Where is a problem? Thanks.

Your request callback function (the function that processes the server response) is run asynchronously, but the validation function returns the value of "valid" (false) immediately.

When the callback function runs (ie after the server responds) the Form.Validator.add() function has already finished executing and the field has been marked as invalid long ago.

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