jqGrid allows only numbers when editing cell

19,011

Solution 1

I don't use jQuery.numeric plugin myself, but I suppose you should use dataInit property of editoptions for the corresponding grid column:

editoptions: { dataInit: function (elem) { 
                   $(elem).numeric(/*some optional parameters*/); 
               }
}

or in case of some trouble in the form

editoptions: { dataInit: function (elem) {
                   setTimeout(function(){
                       $(elem).numeric();
                   }, 100);
               }
}

I hope it will work.

Solution 2

      {name:'actualNo',index:'actualNo',editable:true, edittype:"text", width:150,editoptions:{
                                size: 15, maxlengh: 10,
                                dataInit: function(element) {
                                    $(element).keyup(function(){
                                        var val1 = element.value;
                                        var num = new Number(val1);
                                        if(isNaN(num))
                                        {alert("Please enter a valid number");}
                                    })
                                }
                            }},

Solution 3

{name:'rate',index:'rate', align:"left", width:'150',editable:true,

            edittype:"text", editoptions:{
                size: 25, maxlengh: 30,
                dataInit: function(element) {
                    $(element).keypress(function(e){
                         if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                            return false;
                         }
                    });
                }
            }
        },
Share:
19,011
user590586
Author by

user590586

Updated on June 04, 2022

Comments

  • user590586
    user590586 almost 2 years

    I want to prevent my user from typing letters inside a numeric field. I saw that there is an option of: editrules:{number:true}, but this option will let the user click any key the user wants and only when row saved it will alert for illegal input. This is not good option for me. I want to prevent from the start the typing of keys that are not numbers (for example in a regular input I can use jQuery's .numeric()).

    How can this be done?

  • Aylen
    Aylen over 10 years
    Great answer! If you want to allow only integers you can use $(elem).numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });. See here.
  • Oleg
    Oleg over 10 years
    @Filly: Thanks for the advice! It could be interesting for other readers.
  • rpaillao
    rpaillao over 3 years
    Thanks, more simple i'ts always the best way.