Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7488207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:47:31+00:00 2026-05-29T14:47:31+00:00

I have a jqGrid (pared-down example below) where I need rather complicated validation to

  • 0

I have a jqGrid (pared-down example below) where I need rather complicated validation to be done as new rows are being selected, unselected, and (later) when the grid is saved. The built-in validation techniques weren’t working well for my purposes (for legacy purposes, I need fairly fine-grained control over the behavior). I’ve got almost all of the problems fixed except for the ‘Enter’ key.

This is the “inline editing” demo, except notice the custom “onSelectRow” function…

<script>
jQuery(document).ready(function(){ 
  var lastsel
  jQuery("#rowed5").jqGrid({        
    datatype: "local",
    colNames:['ID Number','Name', 'Stock', 'Ship via','Notes'],
    colModel:[
      {name:'id',index:'id', width:90, sorttype:"int", editable: true},
      {name:'name',index:'name', width:150,editable: true },
      {name:'stock',index:'stock', width:60, editable: true},
      {name:'ship',index:'ship', width:90, editable: true},                       
      {name:'note',index:'note', width:200, sortable:false,editable: true}                      
              ],
    onSelectRow: function(id){
            if (id && id !== lastsel) {
                if (lastsel != undefined) {
                    jQuery("#rowed5").jqGrid('saveRow', lastsel);
                    if ( ! myValidate(lastsel) ) {
                        jQuery("#rowed5").jqGrid('editRow', lastsel, true);
                        return;
                    }
                }
                jQuery("#rowed5").jqGrid('editRow', id, true);
                lastsel = id;
            }
    },
    editurl: "clientArray",
    caption: "Sample"
  });
  var mydata2 = [
    {id:"12345",name:"Desktop Computer",note:"note",stock:"Yes",ship:"FedEx"},
    {id:"23456",name:"Laptop",note:"Long text ",stock:"Yes",ship:"InTime"},
    {id:"34567",name:"LCD Monitor",note:"note3",stock:"Yes",ship:"TNT"},
    ];
  for(var i=0;i<mydata2.length;i++)
    jQuery("#rowed5").addRowData(mydata2[i].id,mydata2[i]);
});

function myValidate(id) {
    var t = jQuery("#rowed5").jqGrid("getCell", id, 'note');
    if ( t.length > 5 ) {
        jQuery("#rowed5")
            .jqGrid('resetSelection').jqGrid('setSelection', id, false);
        alert('note is too long, max 5 char');
        return false;
    }
    return true;
}
</script>
<table id="rowed5"></table>

If you change row selections within the grid by using the mouse the field “notes” is validated. However, if you press the enter key to accept the row values… I can’t figure out where to catch that event to insert my validation function. There doesn’t seem to be an “onSaveRow” event or “before saveRow” event to catch.

It seems I need to either catch the “enter” key myself, or inject my code into the saveRow method

Note: I’m aware of the beforeCellSave event when using cellEdit: true but I don’t want cell editing.

Edit: Now reflects Oleg’s demo with my code largely removed:

    $(document).ready(function () {
        'use strict';
        var mydata = [
                {id: "12345", name: "Desktop Computer", note: "note",      stock: "Yes", ship: "FedEx"},
                {id: "23456", name: "Laptop",           note: "Long text", stock: "Yes", ship: "InTime"},
                {id: "34567", name: "LCD Monitor",      note: "note3",     stock: "Yes", ship: "TNT"}
            ],
            $grid = $("#rowed5"),
            numberTemplate = {formatter: 'number', align: 'right', sorttype: 'number', editable: true,
                searchoptions: { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge', 'nu', 'nn', 'in', 'ni'] }},
            lastsel;

        function myValidate1 (value, colname) {
            if (value.length > 5) {
                return [false, " is too long for the column '" +
                               colname + "'\nMax 5 char is permitted"];
            } else {
                return [true, ""];
            }
        }

        $grid.jqGrid({
            datatype: "local",
            data: mydata,
            height: 'auto',
            colNames: ['ID Number','Name', 'Stock', 'Ship via','Notes'],
            colModel: [
              {name: 'id',    index: 'id',    width: 90,  editable: true, sorttype: "int"},
              {name: 'name',  index: 'name',  width: 150, editable: true},
              {name: 'stock', index: 'stock', width: 60,  editable: true},
              {name: 'ship',  index: 'ship',  width: 90,  editable: true},
              {name: 'note',  index: 'note',  width: 200, editable: true, sortable: false,
                  editrules: {custom: true, custom_func: myValidate1}}
            ],
            onSelectRow: function (id) {
                var $this = $(this);
                if (id && id !== lastsel) {
                    if (lastsel != undefined) {
                        $this.jqGrid('saveRow', lastsel);
                    }
                    $this.jqGrid('editRow', id, true);
                    lastsel = id;
                }
            },
            editurl: "clientArray",
            caption: "Sample"
        });
    });

I removed the myValidate call and the return from the select handler. The problem that remains though is that the demo relies only on the editrules/custom_func validation and it no longer works. When I return [false, ...] from the handler the selection still succeeds and the new line is selected. I need to keep the old line selected if the validation fails.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T14:47:34+00:00Added an answer on May 29, 2026 at 2:47 pm

    First of all I would recommend you to use

    editoptions: {maxlength: 5}
    

    to restrict the max length of the input characters. For the validation you can use additionally editrules. If the example only the demo and you need some really complex validation you consider to use custom validation rule. For example

    editrules: {custom: true, custom_func: myValidate1}
    

    where

    function myValidate1 (value, colname) {
        if (value.length > 5) {
            return [false, " is too long for the column '" +
                           colname + "'\nMax 5 char is permitted"];
        } else {
            return [true, ""];
        }
    }
    

    see the demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a case where I need to update a jqgrid based on some
I have a jqGrid which when a row is selected, a jQuery UI modal
I have jqGrid with two columns, one being hidden. For some reason in FireFox
I have a jqgrid where the database table has a few thousand rows, but
I have a jqGrid with four columns and in which i get the rows
Framework: Javascript, Jqgrid I have the below javascript function. It receives array of fields
I have jqgrid with 4 columns each with checkboxes format, I need to get
I have a jqGrid that when I go into edit mode I need to
I have a jqgrid with a checkbox column. when checkbox is pressed i need
I have a jqGrid where I want all the rows to be in edit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.