0

This thing always return false on me.

$.validator.unobtrusive.adapters.add('appointmentvalidating', ['daylimit'], function (options) {
    options.rules['appointmentvalidating'] = options.params;
    options.messages.appointmentvalidating = options.message;
});
$.validator.addMethod('appointmentvalidating', function (value, element, params) {
    var dtnow = new Date(Date.parse(value));
    var daylimit = parseInt(params.daylimit.toString());
    var count = 0;
    count = request(dtnow);
    if (count < daylimit) {
        console.log("true");
        return true;
    }
    if (count >= daylimit) {
        console.log("true");
        return false;
    }
});
function request(dtnow) {
    $.ajax({
        url: "/api/webapi/",
        type: "GET",
        async: true,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
           var count = 0;
            for (var i = 0; i < data.length; i++) {
                var datentbproc = data[i].Date;
                var datentbproccor = new Date(Date.parse(datentbproc.toString().substr(5, 2) + '/' + datentbproc.toString().substr(8, 2) + '/' + datentbproc.toString().substr(0, 4)));
                if (datentbproccor.getFullYear() == dtnow.getFullYear() && datentbproccor.getMonth() == dtnow.getMonth() && datentbproccor.getDate() == dtnow.getDate()) {
                    count = count + 1;
                }
            }
            return count;
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('request is failed');
        },
        timeout: 120000,
    });
}

But when I use synchronous requests like this it does return false,true as I want.

$.validator.unobtrusive.adapters.add('appointmentvalidating', ['daylimit'], function (options) {
    options.rules['appointmentvalidating'] = options.params;
    options.messages.appointmentvalidating = options.message;
});
$.validator.addMethod('appointmentvalidating', function (value, element, params) {
    var dtnow = new Date(Date.parse(value));
    var daylimit = parseInt(params.daylimit.toString());
    var count = 0;
    $.ajax({
        url: "/api/webapi/",
        type: "GET",
        async: false,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                var datentbproc = data[i].Date;
                var datentbproccor = new Date(Date.parse(datentbproc.toString().substr(5, 2) + '/' + datentbproc.toString().substr(8, 2) + '/' + datentbproc.toString().substr(0, 4)));
                if (datentbproccor.getFullYear() == dtnow.getFullYear() && datentbproccor.getMonth() == dtnow.getMonth() && datentbproccor.getDate() == dtnow.getDate()) {
                    count = count + 1;
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('request is failed');
        },
        timeout: 120000,
    });
    if (count >= daylimit) {
        return false;
    }
    else if (count < daylimit) {
        return true;
    }
});

As you can see I used If block outside in synchronous request. I tried lot of things. Any of them did not work. If someone face the similar problem and solved can help me I think. What to do now?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Felix Kling Jun 21 '15 at 17:53
  • 1
    This is literally the top question in the `ajax` tag: https://stackoverflow.com/questions/tagged/ajax?sort=frequent&pageSize=30. Please search before you ask a new question. – Felix Kling Jun 21 '15 at 17:54
  • @Felix I am using ajax asynchronous call unobtrisive validation. Looks like different. Can you give me a more specific answer? ... – Waruna Karunathialaka Jun 21 '15 at 18:05
  • 1
    You are asking how to return the response from an Ajax call, and that is exactly what the other question is about. Take your time to read and understand the answer. – Felix Kling Jun 21 '15 at 18:18
  • 1
    However extending on that dupe, your `$.validator` plugin needs to support asynchronous validation methods, or this cannot possibly work. Check its documentation - if you don't find anything, you might have to switch your plugin. – Bergi Jun 21 '15 at 18:50
  • @Felix I understood what this problem is. I understood what is asynchronous means. I used functionname().when().done() and I used and setTimeOut anything did not work on my problem. Have you used yourtheory on unobtrusive validation itself? – Waruna Karunathialaka Jun 22 '15 at 06:06
  • I have not used that specific plugin. As Bergi said, if the plugin does not support asynchronous validation methods there is nothing you can do about it. You'd have to look for another plugin. – Felix Kling Jun 22 '15 at 06:18

0 Answers0