Server Validators

Example of using onServerValidateQuestions callback

                        //assign call to onServerValidateQuestions callback
function surveyValidateQuestion(survey, options) {
    //options.data contains the data for the current page.
    var countryName = options.data["country"];
    //If the question is empty then do nothing
    if (!countryName) options.complete();
    //call the ajax method
    $.ajax({
        url: "https://restcountries.eu/rest/v2/all"
    }).then(function (data) {
        var found = false;
        var countries = data;
        for (var i = 0; i < countries.length; i++) {
            if (countries[i].name == countryName) {
                found = true; break;
            }
        }
        //if the country is unknown, add the error
        if (!found) options.errors["country"] = "The country name '" + countryName +"' is not in this list: https://restcountries.eu/rest/v2/all";
        //tell survey that we are done with the server validation
        options.complete();
    });
}

    
        Survey.StylesManager.applyTheme("default");
    


var json = {
    questions: [{ type: "text", name: "country", title: "Type a country:" }]
};

window.survey = new Survey.Model(json);


survey.onComplete.add(function(result) {
    document.querySelector('#surveyResult').innerHTML = "result: " + JSON.stringify(result.data);
});


$("#surveyElement").Survey({ 
    model: survey ,onServerValidateQuestions: surveyValidateQuestion

});


                    
<!DOCTYPE html>
<html>
<head>
    <title>Example of using onServerValidateQuestions callback, jQuery Survey Library Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/jquery"></script>
<script src="https://surveyjs.azureedge.net/1.0.50/survey.jquery.js"></script>
<link href="https://surveyjs.azureedge.net/1.0.50/survey.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">

</head>
<body>
                <div id="surveyElement">
            </div>
    <div id="surveyResult"></div>

<script type="text/javascript" src="./index.js"></script>

</body>
</html>