1

My purpose is to not allow the same user to register again, therefore I'm validating the username against existing usernames in the database. It is working properly but it takes a long while to perform the validation.

(function ($) {
    FormValidation.Validator.existingUserCheck = {
        validate: function (validator, $field, options) {
            var value = $field.val();
            var cnt = 0;
            if (value)
            {
                $.ajax({
                    type: 'post',
                    url: '<?php echo base_url('check - user - exist'); ?>',
                    data: {
                        user_name: value
                    },
                    dataType: 'json',
                    async: false,
                    success: function (response)
                    {
                        if (response === 'true')
                        {
                            cnt = 1;
                        }
                    }
                });
            }
            if (cnt === 1)
            {
                return {
                    valid: false,
                    message: 'Username is taken'
                };
            } else {
                return {
                    valid: true
                };
            }
        }
    };
}(window.jQuery));
skirtle
  • 27,868
  • 4
  • 42
  • 57
  • Please send your PHP code also – usman ikram Oct 19 '17 at 07:08
  • 1
    Did you verify that whether the delay is from javascript or DB (validation api)? – Thaadikkaaran Oct 19 '17 at 07:08
  • Have you tried setting `async: true`, when it's `false` the browser waits for the ajax request to be completed before continuing with the rest of the code. – VTodorov Oct 19 '17 at 07:10
  • when async: true validation will perform without checking with database –  Oct 19 '17 at 07:15
  • –  Oct 19 '17 at 07:16
  • Delay is coming in javascript –  Oct 19 '17 at 07:25
  • `` no file exists – pokeybit Oct 19 '17 at 07:26
  • no ,file exist php part work fine –  Oct 19 '17 at 07:27
  • The logic of a response if true and no response if false will just lag out when false... – pokeybit Oct 19 '17 at 07:30
  • "when async: true validation will perform without checking with database" yes because you're not waiting for the ajax to return before continuing to execute your code. If you need to run something which depends on the result of the ajax call, that code must be written within (or within a function which is called from) the "success" function of the ajax call. See https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – ADyson Oct 19 '17 at 08:59

0 Answers0