Basic

This is the very basic use of the ExpressionBuilder by just enabling the tool with validaiton:

    var expBuilder = $('#txt-1').expressionBuilder();

    $('.btn-validate-1').on('click', function () {
      if (expBuilder.isValid())
        showMessage('Valid');
      else
        showMessage('Not valid');
    });

    $('.btn-get-1').on('click', function () {
      showMessage(expBuilder.getExpression());
    });
  

Variables

In this example we have defined a variable list for the expressionBuidler, type the variable names to see the suggestions:

    let options = {
      variables: [
        {
          variableId: 1,
          name: 'FirstName'
        },
        {
          variableId: 2,
          name: 'Age'
        }
      ],
      expression: "[2] + 4"
    };

    var expBuilder2 = $('#txt-2').expressionBuilder(options);

    $('.btn-validate-2').on('click', function () {
      if (expBuilder2.isValid())
        showMessage('Valid');
      else
        showMessage('Not valid');
    });

    $('.btn-get-exp-2').on('click', function () {
      showMessage(expBuilder2.getExpression());
    });

    $('.btn-get-input-2').on('click', function () {
      showMessage(expBuilder2.getInput());
    });
  

Functions

In this example we have defined two funcitons for the expressionBuidler, type the function names to see the suggestions:

Note that you can add more validation within your functions by threwing an error.

    let options3 = {
      variables: [
        {
          variableId: 1,
          name: 'FirstName'
        },
        {
          variableId: 2,
          name: 'Age',
          value: 25
        }
      ],
      expression: "Add([2], 4)",
      functions: {
        Add: function (x, y) {

          if (x == undefined || isNaN(parseFloat(x)))
            throw new Error("Parameter 'x' is not defined or is not a number.");

          return x + y;
        },
        Sub: function (x, y) {
          return x - y;
        }
      }
    };

    var expBuilder3 = $('#txt-3').expressionBuilder(options3);

    $('.btn-validate-3').on('click', function () {
      if (expBuilder3.isValid())
        showMessage('Valid');
      else
        showMessage('Not valid');
    });

    $('.btn-get-exp-3').on('click', function () {
      showMessage(expBuilder3.getExpression());
    });

    $('.btn-get-run-3').on('click', function () {
      showMessage(expBuilder3.getInput() + " = " + expBuilder3.runExpression());
    });