Add New Row to Top
Add New Row to Bottom

jQuery Grid: Validation

The first field is validated. You should enter more than 5 characters.

A live JavaScript/jQuery example of jqxGrid, the jQWidgets Grid component, with its complete source code below. It is one of 139 jqxGrid demos for jQuery.

jqxGrid API used in this demo

Source code

To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxGrid shows the setup step by step.

The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxlistbox.js, jqxdropdownlist.js, jqxmenu.js, jqxgrid.js, jqxgrid.filter.js, jqxgrid.sort.js, jqxgrid.selection.js, jqxgrid.edit.js, jqxgrid.columnsresize.js, jqxpanel.js, jqxcalendar.js, jqxradiobutton.js, jqxdatetimeinput.js, jqxcheckbox.js.

It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers), generatedata.js (generatedata(), random sample rows).

script.js

$(document).ready(function () {
    var data = generatedata(10);
    var source =
    {
        localdata: data,
        datafields:
        [
            { name: 'name', type: 'string' },
            { name: 'productname', type: 'string' },
            { name: 'available', type: 'bool' },
            { name: 'date', type: 'date'},
            { name: 'quantity', type: 'number' }
        ],
        datatype: "array"
    };

    var dataAdapter = new $.jqx.dataAdapter(source);

    // validation function
    var validateFunc = function (datafield, value) {
        switch (datafield) {
            case "name":
                if (value.length < 5) {
                    return { message: "Entered value should be more than 5 characters", result: false };
                }
                return true;
            case "productname":
                if (value.length == 0) {
                    return { message: "Entered field should be more than 0 characters", result: false };
                }
                return true;
                break;
            case "quantity":
                if (value < 0) {
                    return { message: "Entered quantity should be positive number", result: false };
                }
                return true;
                break;
            case "date":
                if (new Date(value) == "Invalid Date") {
                    return { message: "Entered date should be valid", result: false };
                }
                return true;
                break;
        }
        return true;
    }

    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        filterable: true,
        source: dataAdapter,
        showeverpresentrow: true,
        everpresentrowposition: "top",
        editable: true,
        selectionmode: 'multiplecellsadvanced',
        columns: [
          {
              text: 'Name', columntype: 'textbox', filtertype: 'input', datafield: 'name', width: 215,
              validateEverPresentRowWidgetValue: validateFunc
          },
          {
              text: 'Product', filtertype: 'checkedlist', datafield: 'productname', width: 220, validateEverPresentRowWidgetValue: validateFunc
          },
          { text: 'Ship Date', datafield: 'date', filtertype: 'range', width: 210, cellsalign: 'right', cellsformat: 'd', validateEverPresentRowWidgetValue: validateFunc },
          { text: 'Qty.', datafield: 'quantity', filtertype: 'number', cellsalign: 'right', validateEverPresentRowWidgetValue: validateFunc }
        ]
    });

    $("#top").jqxRadioButton({ checked: true });
    $("#bottom").jqxRadioButton();
    $("#top").on('checked', function () {
        $("#grid").jqxGrid('everpresentrowactions', 'add reset');
    });
    $("#bottom").on('checked', function () {
        $("#grid").jqxGrid('everpresentrowactions', 'addBottom reset');
    });
});

body.html

<div id="grid">
</div><br>
<div id="top">Add New Row to Top</div>
<div id="bottom">Add New Row to Bottom</div>

Learn more

More jQuery Grid demos