Cart Licenses Contact Support Online Creator Log in/Register
v 1.9.43

Expression Validator with async functions

            Survey.defaultBootstrapCss.navigationButton = "btn btn-green";

//Function that returns the value on callback async
function isCountryExist(params) {
    if (params.length < 1) return false;
    var countryName = params[0];
    //If the question is empty then do nothing
    if (!countryName) {
        //It doesn't matter what the function returns. The library is wating for this.returnResult(resultValue) callback
        return false;
    var self = this;
    //call the ajax method
    $.ajax({url: "" + countryName}).then(function (data) {
        var found = data.length > 0;
        // return the value into the library. Library is waiting for this callback
    //May return any value. The library will ignore it.
    return false;

// It is important to tell the library that the function is async, the last parameter
// and you will return the value on this.returnResult callback
Survey.FunctionFactory.Instance.register("isCountryExist", isCountryExist, true);

If you do not need to support es5 (IE), then you may use async/await operators
async function isCountryExist(params) {
    if (params.length < 1) return false;
    var countryName = params[0];
    if (!countryName) {
        return false;
    var self = this;
    var res = await $.ajax({
        url: ""
    }).then(function(data) {
        var found = false;
        var countries = data;
        for (var i = 0; i < countries.length; i++) {
        if (countries[i].name == countryName) {
            found = true;
    return false;

                    var json = {
  elements: [
      type: "text",
      name: "country",
      title: "Type a country:",
      validators: [
          type: "expression",
          expression: "isCountryExist({country}) = true",
          text: "Please type the country correctly"
        type: "comment",
        name: "aboutCountry",
        title: "Please tell us about country: '{country}'",
        visibleIf: "isCountryExist({country}) = true"

                    window.survey = new Survey.Model(json);
            survey.onComplete.add(function(sender) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(, null, 3);
            model: survey 

<!DOCTYPE html>
<html lang="en">
    <title>Expression Validator with async functions, jQuery Survey Library Example</title>
<meta name="viewport" content="width=device-width" />
    <script src=""></script>
    <script src="/DevBuilds/survey-jquery/survey.jquery.min.js"></script>
    <link rel="stylesheet" href="">
    <link rel="stylesheet" href="./index.css">

<body style="margin: 0">
            <div id="surveyElement" style="display:inline-block;width:100%;">
    <div id="surveyResult"></div>

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


Tell Us What You Think

Help us serve you better by taking this brief survey.
We are interested to learn more about your
experience of using our libraries.

We'd really appreciate your feedback.

Start the Survey

Approximate time to complete: 2 min.